JTables

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jun 2004
Posts: 609
Reputation: freesoft_2000 is an unknown quantity at this point 
Solved Threads: 8
freesoft_2000 freesoft_2000 is offline Offline
Practically a Master Poster

JTables

 
0
  #1
Feb 8th, 2005
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

  1.  
  2. import java.awt.*;
  3. import java.io.*;
  4. import java.util.*;
  5. import java.awt.print.*;
  6. import java.awt.event.*;
  7. import javax.swing.*;
  8. import javax.swing.event.*;
  9. import javax.swing.table.*;
  10.  
  11. public class JTablesui implements ActionListener, ItemListener
  12.  
  13. {
  14.  
  15. JFrame fr = new JFrame ("Frame");
  16. JDialog Dialog1 = new JDialog (fr, "Row Height");
  17.  
  18. JLabel Label1 = new JLabel("Label1 ", SwingConstants.RIGHT);
  19. JLabel Label2 = new JLabel(" ", SwingConstants.LEFT);
  20. JLabel Label3 = new JLabel("Enter table row height in pixels");
  21.  
  22. JButton Button6 = new JButton("Save As");
  23. JButton Button19 = new JButton("Set Row Height");
  24. JButton Button20 = new JButton("Set Height");
  25. JButton Button21 = new JButton("Close");
  26. JButton Button22 = new JButton("Foreground Color For Cells");
  27.  
  28. JTextField TextField1 = new JTextField("", 10);
  29. JTextField TextField2 = new JTextField("", 20);
  30. JTextField TextField3 = new JTextField("", 10);
  31.  
  32. JComboBox ComboBox1;
  33. JComboBox ComboBox2 = new JComboBox();
  34.  
  35. //The below command line sets the model for the JTable which
  36. //has two arguments as explained below
  37. //The first argument specifies the number of rows for the JTable to display upon
  38. //it's initialization
  39. //The second argument specifies the number of columns for the JTable to display upon
  40. //it's initialization
  41.  
  42. DefaultTableModel TableModel1 = new DefaultTableModel(5, 10);
  43.  
  44. //The below command line sets the table model to the JTable
  45.  
  46. JTable Table1 = new JTable(TableModel1);
  47.  
  48. JScrollPane ScrollPane1 = new JScrollPane(Table1, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
  49. ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
  50.  
  51. JFileChooser FileChooser1 = new JFileChooser();
  52.  
  53. JColorChooser ColorChooser3 = new JColorChooser();
  54.  
  55.  
  56. Color Color3;
  57.  
  58. Dimension Size1 = new Dimension();
  59.  
  60. int FontSize = 12;
  61. String FontFamily = "Arial";
  62.  
  63. String SF = "";
  64.  
  65. public void initialize ()
  66. {
  67. Container pane = fr.getContentPane();
  68. pane.setLayout(new FlowLayout());
  69. fr.setSize(250,300);
  70. fr.setLocation(300,300);
  71. fr.setBackground(Color.lightGray);
  72. //The below command line must be set to false so that user
  73. //resizing is allowed
  74.  
  75. Table1.setAutoCreateColumnsFromModel(false);
  76. Size1.width = 350;
  77. Size1.height = 250;
  78. ScrollPane1.setPreferredSize(Size1);
  79. Table1.setModel(TableModel1);
  80. //The below command line must be set to JTable.AUTO_RESIZE_OFF so that user
  81. //resizing is allowed
  82.  
  83. Table1.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
  84. pane.add(ScrollPane1);
  85. pane.add(Button6);
  86. pane.add(Button19);
  87. pane.add(Button22);
  88. combofontfamilyinitialize();
  89. pane.add(ComboBox1);
  90. combofontsizeinitialize();
  91. pane.add(ComboBox2);
  92. pane.add(Label1);
  93.  
  94. // Use the JAVA constant JFrame.HIDE_ON_CLOSE for subsequent forms in multi form
  95. // applications
  96.  
  97. fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  98. Button6.addActionListener(this);
  99. Button19.addActionListener(this);
  100. Button22.addActionListener(this);
  101. ComboBox1.addItemListener(this);
  102. ComboBox2.addItemListener(this);
  103. fr.pack();
  104. fr.setVisible(true);
  105. }
  106.  
  107. public void combofontfamilyinitialize ()
  108. {
  109. //This function fills the combo box with the system available font families
  110.  
  111. GraphicsEnvironment ge1 = GraphicsEnvironment.getLocalGraphicsEnvironment();
  112. String[] k = ge1.getAvailableFontFamilyNames();
  113. ComboBox1= new JComboBox(k);
  114. }
  115.  
  116. public void combofontsizeinitialize ()
  117. {
  118. //This function fills the combo box with font sizes
  119.  
  120. ComboBox2.addItem("8");
  121. ComboBox2.addItem("9");
  122. ComboBox2.addItem("10");
  123. ComboBox2.addItem("11");
  124. ComboBox2.addItem("12");
  125. ComboBox2.addItem("14");
  126. ComboBox2.addItem("16");
  127. ComboBox2.addItem("18");
  128. ComboBox2.addItem("20");
  129. ComboBox2.addItem("22");
  130. ComboBox2.addItem("24");
  131. ComboBox2.addItem("26");
  132. ComboBox2.addItem("28");
  133. ComboBox2.addItem("36");
  134. ComboBox2.addItem("48");
  135. ComboBox2.addItem("72");
  136. }
  137.  
  138. public void initializedialog1 ()
  139. {
  140. Container pane1 = Dialog1.getContentPane();
  141. pane1.setLayout(new BoxLayout(pane1, BoxLayout.Y_AXIS));
  142. Dialog1.setSize(250,300);
  143. Dialog1.setLocation(300,300);
  144. Dialog1.setBackground(Color.lightGray);
  145. pane1.add(Label3);
  146. pane1.add(TextField3);
  147. pane1.add(Button20);
  148. pane1.add(Button21);
  149.  
  150. //Use the JAVA constant JDialog.HIDE_ON_CLOSE for subsequent forms in multi form
  151. //applications
  152.  
  153. Dialog1.setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE);
  154. Button20.addActionListener(this);
  155. Button21.addActionListener(this);
  156. Dialog1.pack();
  157. Dialog1.setVisible(true);
  158. }
  159.  
  160. public void tablesaveas ()
  161. {
  162. //This function saves the model of the table to disk using the user
  163. //specified file name
  164.  
  165. //This is the function in which the exception is thrown
  166.  
  167. if(FileChooser1.showSaveDialog(fr) != JFileChooser.APPROVE_OPTION)
  168. {
  169. return;
  170. }
  171.  
  172. try
  173. {
  174. File f = FileChooser1.getSelectedFile();
  175. SF = (f.toString() + ".DAT");
  176. FileOutputStream fStream = new FileOutputStream(SF);
  177. ObjectOutput stream = new ObjectOutputStream(fStream);
  178.  
  179. //The below two command lines saves the table model and table column data
  180. //as an object using the user specified file name
  181. stream.writeObject(Table1.getModel());
  182. stream.writeObject(Table1.getColumnModel());
  183. stream.writeObject(Table1.getFont());
  184. stream.writeObject(Table1.getForeground());
  185. stream.writeObject(TextField3.getText());
  186. stream.writeObject(Label2);
  187. stream.flush();
  188. stream.close();
  189. fStream.close();
  190. }
  191.  
  192. catch (Exception e)
  193. {
  194. Label1.setText("A table saving error has occurred");
  195. }
  196.  
  197. }
  198.  
  199. public void actionPerformed(ActionEvent event)
  200. {
  201. JComponent b = (JComponent)event.getSource();
  202. int d, d1;
  203. String str3;
  204.  
  205. if(b == Button6)
  206. {
  207. //This is the function in which the exception is thrown
  208.  
  209. tablesaveas();
  210. }
  211.  
  212. else if(b == Button19)
  213. {
  214. d = Table1.getRowHeight();
  215. str3 = Integer.toString(d);
  216. TextField3.setText(str3);
  217. initializedialog1();
  218. }
  219.  
  220. else if(b == Button20)
  221. {
  222. str3 = TextField3.getText();
  223. d = Integer.parseInt(str3);
  224. Table1.setRowHeight(d);
  225. }
  226.  
  227. else if(b == Button21)
  228. {
  229. Dialog1.setVisible(false);
  230. }
  231.  
  232. else if(b == Button22)
  233. {
  234. Color3 = ColorChooser3.showDialog(fr, "Color Chooser", Color.black);
  235. //The below command line test to see if the Ok button or the Cancel button
  236. //was clicked on the JColorChooser as if no color is selected the JColorChooser
  237. //simply returns a null
  238.  
  239. if(Color3 != null)
  240. {
  241. Table1.editCellAt(0,0);
  242. TextField1 = (JTextField)Table1.getEditorComponent();
  243.  
  244. if(TextField1 != null)
  245. {
  246. TextField1.setForeground(Color3);
  247. }
  248.  
  249. Table1.setSelectionForeground(Color3);
  250. Table1.setForeground(Color3);
  251.  
  252. }
  253.  
  254. }
  255.  
  256. }
  257.  
  258. public void itemStateChanged(ItemEvent event)
  259. {
  260. JComponent c = (JComponent)event.getSource();
  261.  
  262. if(c == ComboBox1)
  263. {
  264. Table1.editCellAt(0,0);
  265. FontFamily = (String)ComboBox1.getSelectedItem();
  266. Font Font1 = new Font(FontFamily, Font.PLAIN, FontSize);
  267. TextField1 = (JTextField)Table1.getEditorComponent();
  268.  
  269. if(TextField1 != null)
  270. {
  271. TextField1.setFont(Font1);
  272. }
  273.  
  274. Table1.setFont(Font1);
  275. }
  276.  
  277. else if(c == ComboBox2)
  278. {
  279. String h = (String)ComboBox2.getSelectedItem();
  280. FontSize = Integer.parseInt(h);
  281. Table1.editCellAt(0,0);
  282. Font Font2 = new Font(FontFamily, Font.PLAIN, FontSize);
  283. TextField1 = (JTextField)Table1.getEditorComponent();
  284.  
  285. if(TextField1 != null)
  286. {
  287. TextField1.setFont(Font2);
  288. }
  289.  
  290. Table1.setFont(Font2);
  291. }
  292.  
  293. }
  294. public static void main(String args[])
  295. {
  296. JTablesui a = new JTablesui();
  297. a.initialize();
  298. }
  299. }

This is the exception that is thrown. The exception does not seem to be thrown from my own code.

Here is the exception thrown

  1.  
  2. java.lang.NullPointerException
  3. at javax.swing.plaf.basic.BasicTableUI.getPreferredSize(BasicTableUI.java:908)
  4. at javax.swing.JComponent.getPreferredSize(JComponent.java:1275)
  5. at javax.swing.ScrollPaneLayout.layoutContainer(ScrollPaneLayout.java:769)
  6. at java.awt.Container.layout(Container.java:1020)
  7. at java.awt.Container.doLayout(Container.java:1010)
  8. at java.awt.Container.validateTree(Cojava.lang.NullPointerException
  9. at javax.swing.plaf.basic.BasicTableUI.paint(BasicTableUI.java:939)
  10. at javax.swing.plaf.ComponentUI.update(ComponentUI.java:142)
  11. at javax.swing.JComponent.paintComponent(JComponent.java:541)
  12. at javax.swing.JComponent.paint(JComponent.java:808)
  13. at javax.swing.JComponent.paintChildren(JComponent.java:647)
  14. at javax.swing.JComponent.paint(JComponent.java:817)
  15. at javax.swing.JViewport.paint(JViewport.java:722)
  16. at javax.swing.JComponent.paintChildren(JComponent.java:647)
  17. at javax.swing.JComponent.paint(JComponent.java:817)
  18. at javax.swing.JComponent.paintChildren(JComponent.java:647)
  19. at javax.swing.JComponent.paint(JComponent.java:817)
  20. at javax.swing.JComponent.paintChildren(JComponent.java:647)
  21. at javax.swing.JComponent.paint(JComponent.java:817)
  22. at javax.swing.JLayeredPane.paint(JLayeredPane.java:557)
  23. at javax.swing.JComponent.paintChildren(JComponent.java:647)
  24. at javax.swing.JComponent.paintWithOffscreenBuffer(JComponent.java:4794)
  25. at javax.swing.JComponent.paintDoubleBuffered(JComponent.java:4740)
  26. at javax.swing.JComponent.paint(JComponent.java:798)
  27. at java.awt.GraphicsCallback$PaintCallback.run(GraphicsCallback.java:21)
  28. at sun.awt.SunGraphicsCallback.runOneComponent(SunGraphicsCallback.java:60)
  29. at sun.awt.SunGraphicsCallback.runComponents(SunGraphicsCallback.java:97)
  30. at java.awt.Container.paint(Container.java:1312)
  31. at sun.awt.RepaintArea.paint(RepaintArea.java:177)
  32. at sun.awt.windows.WComponentPeer.handleEvent(WComponentPeer.java:260)
  33. at java.awt.Component.dispatchEventImpl(Component.java:3678)
  34. at java.awt.Container.dispatchEventImpl(Container.java:1627)
  35. at java.awt.Window.dispatchEventImpl(Window.java:1606)
  36. at java.awt.Component.dispatchEvent(Component.java:3477)
  37. at java.awt.EventQueue.dispatchEvent(EventQueue.java:456)
  38. at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:201)
  39. at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151)
  40. at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:145)
  41. at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:137)
  42. 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

  1.  
  2. public void tablesaveas ()
  3. {
  4. //This function saves the model of the table to disk using the user
  5. //specified file name
  6.  
  7. //This is the function in which the exception is thrown
  8.  
  9. if(FileChooser1.showSaveDialog(fr) != JFileChooser.APPROVE_OPTION)
  10. {
  11. return;
  12. }
  13.  
  14. try
  15. {
  16. File f = FileChooser1.getSelectedFile();
  17. SF = (f.toString() + ".DAT");
  18. FileOutputStream fStream = new FileOutputStream(SF);
  19. ObjectOutput stream = new ObjectOutputStream(fStream);
  20.  
  21. //The below two command lines saves the table model and table column data
  22. //as an object using the user specified file name
  23. stream.writeObject(Table1.getModel());
  24. stream.writeObject(Table1.getColumnModel());
  25. stream.flush();
  26. stream.close();
  27. fStream.close();
  28. }
  29.  
  30. catch (Exception e)
  31. {
  32. Label1.setText("A table saving error has occurred");
  33. }
  34.  
  35. }

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
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,143
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 212
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: JTables

 
0
  #2
Feb 8th, 2005
If you get the exception when trying to write the font, it seems like the getFont() method in JTable somehow causes an attempt to repaint the user interface with incorrect settings.

It may be good to check out the official bug database and/or dive deep into the sourcecode for Swing and AWT.
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 609
Reputation: freesoft_2000 is an unknown quantity at this point 
Solved Threads: 8
freesoft_2000 freesoft_2000 is offline Offline
Practically a Master Poster

Re: JTables

 
0
  #3
Feb 8th, 2005
Hi everyone,

Originally Posted by jwenting
If you get the exception when trying to write the font, it seems like the getFont() method in JTable somehow causes an attempt to repaint the user interface with incorrect settings
Did you run my program and also got the same exception?
What jdk are you running?

Originally Posted by jwenting
It may be good to check out the official bug database.
I already did that and it seems that no such bug exists.

Is there a way that repainting can be turned off?

Thank You

Yours Sincerely

Richard West
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC