Hi everyone,
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.
Here is the code
import java.awt.*;
import java.io.*;
import java.util.*;
import java.awt.print.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.*;
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
Table1.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
pane.add(ScrollPane1);
pane.add(Button6);
pane.add(Button19);
pane.add(Button22);
combofontfamilyinitialize();
pane.add(ComboBox1);
combofontsizeinitialize();
pane.add(ComboBox2);
pane.add(Label1);
// Use the JAVA constant JFrame.HIDE_ON_CLOSE for subsequent forms in multi form
// applications
fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Button6.addActionListener(this);
Button19.addActionListener(this);
Button22.addActionListener(this);
ComboBox1.addItemListener(this);
ComboBox2.addItemListener(this);
fr.pack();
fr.setVisible(true);
}
public void combofontfamilyinitialize ()
{
//This function fills the combo box with the system available font families
GraphicsEnvironment ge1 = GraphicsEnvironment.getLocalGraphicsEnvironment();
String[] k = ge1.getAvailableFontFamilyNames();
ComboBox1= new JComboBox(k);
}
public void combofontsizeinitialize ()
{
//This function fills the combo box with font sizes
ComboBox2.addItem("8");
ComboBox2.addItem("9");
ComboBox2.addItem("10");
ComboBox2.addItem("11");
ComboBox2.addItem("12");
ComboBox2.addItem("14");
ComboBox2.addItem("16");
ComboBox2.addItem("18");
ComboBox2.addItem("20");
ComboBox2.addItem("22");
ComboBox2.addItem("24");
ComboBox2.addItem("26");
ComboBox2.addItem("28");
ComboBox2.addItem("36");
ComboBox2.addItem("48");
ComboBox2.addItem("72");
}
public void initializedialog1 ()
{
Container pane1 = Dialog1.getContentPane();
pane1.setLayout(new BoxLayout(pane1, BoxLayout.Y_AXIS));
Dialog1.setSize(250,300);
Dialog1.setLocation(300,300);
Dialog1.setBackground(Color.lightGray);
pane1.add(Label3);
pane1.add(TextField3);
pane1.add(Button20);
pane1.add(Button21);
//Use the JAVA constant JDialog.HIDE_ON_CLOSE for subsequent forms in multi form
//applications
Dialog1.setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE);
Button20.addActionListener(this);
Button21.addActionListener(this);
Dialog1.pack();
Dialog1.setVisible(true);
}
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
if(FileChooser1.showSaveDialog(fr) != JFileChooser.APPROVE_OPTION)
{
return;
}
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();
}
catch (Exception e)
{
Label1.setText("A table saving error has occurred");
}
}
public void actionPerformed(ActionEvent event)
{
JComponent b = (JComponent)event.getSource();
int d, d1;
String str3;
if(b == Button6)
{
//This is the function in which the exception is thrown
tablesaveas();
}
else if(b == Button19)
{
d = Table1.getRowHeight();
str3 = Integer.toString(d);
TextField3.setText(str3);
initializedialog1();
}
else if(b == Button20)
{
str3 = TextField3.getText();
d = Integer.parseInt(str3);
Table1.setRowHeight(d);
}
else if(b == Button21)
{
Dialog1.setVisible(false);
}
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
if(Color3 != null)
{
Table1.editCellAt(0,0);
TextField1 = (JTextField)Table1.getEditorComponent();
if(TextField1 != null)
{
TextField1.setForeground(Color3);
}
Table1.setSelectionForeground(Color3);
Table1.setForeground(Color3);
}
}
}
public void itemStateChanged(ItemEvent event)
{
JComponent c = (JComponent)event.getSource();
if(c == ComboBox1)
{
Table1.editCellAt(0,0);
FontFamily = (String)ComboBox1.getSelectedItem();
Font Font1 = new Font(FontFamily, Font.PLAIN, FontSize);
TextField1 = (JTextField)Table1.getEditorComponent();
if(TextField1 != null)
{
TextField1.setFont(Font1);
}
Table1.setFont(Font1);
}
else if(c == ComboBox2)
{
String h = (String)ComboBox2.getSelectedItem();
FontSize = Integer.parseInt(h);
Table1.editCellAt(0,0);
Font Font2 = new Font(FontFamily, Font.PLAIN, FontSize);
TextField1 = (JTextField)Table1.getEditorComponent();
if(TextField1 != null)
{
TextField1.setFont(Font2);
}
Table1.setFont(Font2);
}
}
public static void main(String args[])
{
JTablesui a = new JTablesui();
a.initialize();
}
}
This is the exception that is thrown. The exception does not seem to be thrown from my own code.
Here is the exception thrown
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
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
if(FileChooser1.showSaveDialog(fr) != JFileChooser.APPROVE_OPTION)
{
return;
}
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();
}
catch (Exception e)
{
Label1.setText("A table saving error has occurred");
}
}
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.
I am using JDK 1.4.2_04
I hope someone can help me with this problem
Any help is greatly appreciated
Thank You
Yours Sincerely
Richard West