I am trying to save the contents the of the jtable together with all its fonts and everything else. The program compiles without any errors but When i try to save the contents of the JTable an exception is thrown in the tablesaveas method in the below method
Please note that i am saving the jtable as an object. I am providing the below runnable example so you guys can compile and run and see what i mean.
public class JTablesui implements ActionListener, ItemListener
{
JFrame fr = new JFrame ("Frame");
JDialog Dialog1 = new JDialog (fr, "Row Height");
JLabel Label1 = new JLabel("Label1 ", SwingConstants.RIGHT);
JLabel Label2 = new JLabel(" ", SwingConstants.LEFT);
JLabel Label3 = new JLabel("Enter table row height in pixels");
JButton Button6 = new JButton("Save As");
JButton Button19 = new JButton("Set Row Height");
JButton Button20 = new JButton("Set Height");
JButton Button21 = new JButton("Close");
JButton Button22 = new JButton("Foreground Color For Cells");
JTextField TextField1 = new JTextField("", 10);
JTextField TextField2 = new JTextField("", 20);
JTextField TextField3 = new JTextField("", 10);
JComboBox ComboBox1;
JComboBox ComboBox2 = new JComboBox();
//The below command line sets the model for the JTable which
//has two arguments as explained below
//The first argument specifies the number of rows for the JTable to display upon
//it's initialization
//The second argument specifies the number of columns for the JTable to display upon
//it's initialization
DefaultTableModel TableModel1 = new DefaultTableModel(5, 10);
//The below command line sets the table model to the JTable
JTable Table1 = new JTable(TableModel1);
JScrollPane ScrollPane1 = new JScrollPane(Table1, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
JFileChooser FileChooser1 = new JFileChooser();
JColorChooser ColorChooser3 = new JColorChooser();
Color Color3;
Dimension Size1 = new Dimension();
int FontSize = 12;
String FontFamily = "Arial";
String SF = "";
public void initialize ()
{
Container pane = fr.getContentPane();
pane.setLayout(new FlowLayout());
fr.setSize(250,300);
fr.setLocation(300,300);
fr.setBackground(Color.lightGray);
//The below command line must be set to false so that user
//resizing is allowed
Table1.setAutoCreateColumnsFromModel(false);
Size1.width = 350;
Size1.height = 250;
ScrollPane1.setPreferredSize(Size1);
Table1.setModel(TableModel1);
//The below command line must be set to JTable.AUTO_RESIZE_OFF so that user
//resizing is allowed
try
{
File f = FileChooser1.getSelectedFile();
SF = (f.toString() + ".DAT");
FileOutputStream fStream = new FileOutputStream(SF);
ObjectOutput stream = new ObjectOutputStream(fStream);
//The below two command lines saves the table model and table column data
//as an object using the user specified file name
stream.writeObject(Table1.getModel());
stream.writeObject(Table1.getColumnModel());
stream.writeObject(Table1.getFont());
stream.writeObject(Table1.getForeground());
stream.writeObject(TextField3.getText());
stream.writeObject(Label2);
stream.flush();
stream.close();
fStream.close();
}
else if(b == Button22)
{
Color3 = ColorChooser3.showDialog(fr, "Color Chooser", Color.black);
//The below command line test to see if the Ok button or the Cancel button
//was clicked on the JColorChooser as if no color is selected the JColorChooser
//simply returns a null
}
public static void main(String args[])
{
JTablesui a = new JTablesui();
a.initialize();
}
}
To be continued on the below thread as this post is longer than
10000 characters, i apologize
02-08-2005, 08:27 AM
freesoft_2000
Continued from the above thread
This is the exception that is thrown. The exception does not seem to be thrown from my own code.
Here is the exception thrown
Code:
java.lang.NullPointerException
at javax.swing.plaf.basic.BasicTableUI.getPreferredSize(BasicTableUI.java:908)
at javax.swing.JComponent.getPreferredSize(JComponent.java:1275)
at javax.swing.ScrollPaneLayout.layoutContainer(ScrollPaneLayout.java:769)
at java.awt.Container.layout(Container.java:1020)
at java.awt.Container.doLayout(Container.java:1010)
at java.awt.Container.validateTree(Cojava.lang.NullPointerException
at javax.swing.plaf.basic.BasicTableUI.paint(BasicTableUI.java:939)
at javax.swing.plaf.ComponentUI.update(ComponentUI.java:142)
at javax.swing.JComponent.paintComponent(JComponent.java:541)
at javax.swing.JComponent.paint(JComponent.java:808)
at javax.swing.JComponent.paintChildren(JComponent.java:647)
at javax.swing.JComponent.paint(JComponent.java:817)
at javax.swing.JViewport.paint(JViewport.java:722)
at javax.swing.JComponent.paintChildren(JComponent.java:647)
at javax.swing.JComponent.paint(JComponent.java:817)
at javax.swing.JComponent.paintChildren(JComponent.java:647)
at javax.swing.JComponent.paint(JComponent.java:817)
at javax.swing.JComponent.paintChildren(JComponent.java:647)
at javax.swing.JComponent.paint(JComponent.java:817)
at javax.swing.JLayeredPane.paint(JLayeredPane.java:557)
at javax.swing.JComponent.paintChildren(JComponent.java:647)
at javax.swing.JComponent.paintWithOffscreenBuffer(JComponent.java:4794)
at javax.swing.JComponent.paintDoubleBuffered(JComponent.java:4740)
at javax.swing.JComponent.paint(JComponent.java:798)
at java.awt.GraphicsCallback$PaintCallback.run(GraphicsCallback.java:21)
at sun.awt.SunGraphicsCallback.runOneComponent(SunGraphicsCallback.java:60)
at sun.awt.SunGraphicsCallback.runComponents(SunGraphicsCallback.java:97)
at java.awt.Container.paint(Container.java:1312)
at sun.awt.RepaintArea.paint(RepaintArea.java:177)
at sun.awt.windows.WComponentPeer.handleEvent(WComponentPeer.java:260)
at java.awt.Component.dispatchEventImpl(Component.java:3678)
at java.awt.Container.dispatchEventImpl(Container.java:1627)
at java.awt.Window.dispatchEventImpl(Window.java:1606)
at java.awt.Component.dispatchEvent(Component.java:3477)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:456)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:145)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:137)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:100)
As you can see from my above code in the tablesaveas method i am saving
six objects but if i change the tablesaveas method to this(see the below code) the table is saved without any problems
Code:
public void tablesaveas ()
{
//This function saves the model of the table to disk using the user
//specified file name
//This is the function in which the exception is thrown
try
{
File f = FileChooser1.getSelectedFile();
SF = (f.toString() + ".DAT");
FileOutputStream fStream = new FileOutputStream(SF);
ObjectOutput stream = new ObjectOutputStream(fStream);
//The below two command lines saves the table model and table column data
//as an object using the user specified file name
stream.writeObject(Table1.getModel());
stream.writeObject(Table1.getColumnModel());
stream.flush();
stream.close();
fStream.close();
}
The only difference as you can see is that i am writing two objects instead of six and i don't really know what's wrong . I don't know if this is a bug in Java or am i missing something.