943,945 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 22054
  • Java RSS
Nov 20th, 2006
0

JTable Cell Renderers

Expand Post »
Hi everyone,

I am currently trying to use a JTextPane as a cell renderers for a JTable but it does not seem to work although the program compiles. I alsways get an error stating class cast exception saying that i must cast the editor component to JTextField instead of a JTextPane although i am using a JTextPane as a cell renderer.

This exeption gets thrown when i try to apply some font to the selected text in the JTextPane.

Below is a small compilable that i have done which compiles and throws the exception that i have mentioned about

Here is the compilable example

Java Syntax (Toggle Plain Text)
  1. import java.awt.*;
  2. import java.awt.image.*;
  3. import java.io.*;
  4. import java.text.*;
  5. import java.awt.event.*;
  6. import java.awt.geom.*;
  7. import javax.swing.*;
  8. import javax.swing.event.*;
  9. import javax.swing.table.*;
  10. import javax.swing.text.*;
  11.  
  12. public class TabTest implements ActionListener, ItemListener
  13.  
  14. {
  15.  
  16. JFrame fr = new JFrame ("Frame");
  17.  
  18.  
  19. JButton Button1 = new JButton("Add Coloum");
  20. JButton Button2 = new JButton("Add Row");
  21.  
  22. JComboBox ComboBox1;
  23.  
  24. DefaultTableModel TableModel1 = new DefaultTableModel(0, 0);
  25.  
  26. JTable Table1 = new JTable(TableModel1);
  27.  
  28. JScrollPane ScrollPane1 = new JScrollPane(Table1, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
  29. ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
  30.  
  31. String FontFamily = "Arial";
  32.  
  33. Dimension Size1 = new Dimension();
  34.  
  35. //add
  36. //The below command line is the constructor for the JTextPane
  37.  
  38. JTextPane TextPane1 = new JTextPane();
  39.  
  40. //The below two command lines creates instances for fonts
  41.  
  42. SimpleAttributeSet sas = new SimpleAttributeSet();
  43.  
  44. StyleContext sc = new StyleContext();
  45.  
  46. //The below command line sets up the variable for font updating
  47.  
  48. MutableAttributeSet mas;
  49.  
  50. //The below command line is the default document class which
  51. //has one argument as explained below
  52. //The first argument sets the Style Context of the styled document
  53.  
  54. DefaultStyledDocument dse = new DefaultStyledDocument(sc);
  55. StyledEditorKit StyledEditorKit1 = new StyledEditorKit();
  56.  
  57. CellPaneRenderer CellPaneRenderer1 = new CellPaneRenderer();
  58. //end
  59.  
  60. public void initialize ()
  61. {
  62. Container pane = fr.getContentPane();
  63. pane.setLayout(new FlowLayout());
  64. fr.setSize(250,300);
  65. fr.setLocation(300,300);
  66. fr.setBackground(Color.lightGray);
  67. //The below command line must be set to false so that user
  68. //resizing is allowed
  69.  
  70. Table1.setAutoCreateColumnsFromModel(false);
  71. Table1.setGridColor(Color.black);
  72.  
  73. Size1.width = 350;
  74. Size1.height = 250;
  75. ScrollPane1.setPreferredSize(Size1);
  76.  
  77. Table1.setModel(TableModel1);
  78. Table1.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
  79. Table1.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
  80.  
  81. Table1.setDefaultRenderer(Object.class, new CustomTableCellRenderer(Color.white));
  82. Table1.setDefaultRenderer(Object.class, new CellPaneRenderer());
  83.  
  84. pane.add(ScrollPane1);
  85. pane.add(Button1);
  86. pane.add(Button2);
  87. combofontfamilyinitialize();
  88. pane.add(ComboBox1);
  89.  
  90. fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  91. Button1.addActionListener(this);
  92. Button2.addActionListener(this);
  93.  
  94. ComboBox1.addItemListener(this);
  95. fr.pack();
  96. fr.setVisible(true);
  97. }
  98.  
  99. public void combofontfamilyinitialize ()
  100. {
  101. //This function fills the combo box with the system available font families
  102.  
  103. GraphicsEnvironment ge1 = GraphicsEnvironment.getLocalGraphicsEnvironment();
  104. String[] k = ge1.getAvailableFontFamilyNames();
  105. ComboBox1= new JComboBox(k);
  106. }
  107.  
  108.  
  109. public void setAttributeSet(AttributeSet attr)
  110. {
  111.  
  112. //This function only set the specified font set by the
  113. //attr variable to the text selected by the mouse
  114.  
  115. int xStart, xFinish, k;
  116.  
  117. xStart = TextPane1.getSelectionStart();
  118. xFinish = TextPane1.getSelectionEnd();
  119. k = xFinish - xStart;
  120.  
  121. if(xStart != xFinish)
  122. {
  123. dse.setCharacterAttributes(xStart, k, attr, false);
  124. }
  125.  
  126. else if(xStart == xFinish)
  127. {
  128. //The below two command line updates the JTextPane according to what
  129. //font that is being selected at a particular moment
  130.  
  131. mas = StyledEditorKit1.getInputAttributes();
  132. mas.addAttributes(attr);
  133. }
  134.  
  135. }
  136.  
  137. public void insertcolumn (JTable table2)
  138. {
  139. //This function adds a column dynamically to the end of the JTable
  140.  
  141. TableModel1 = (DefaultTableModel)table2.getModel();
  142. TableColumn col = new TableColumn(TableModel1.getColumnCount());
  143.  
  144. //add
  145. col.setCellRenderer(CellPaneRenderer1);
  146. //end
  147.  
  148. TableModel1.addColumn(" ");
  149. //The below command line adds the new column to the JTable
  150.  
  151. table2.addColumn(col);
  152.  
  153. TableModel1.fireTableStructureChanged();
  154. }
  155.  
  156. public void actionPerformed(ActionEvent event)
  157. {
  158. JComponent b = (JComponent)event.getSource();
  159. int d;
  160. String str3 = null;
  161. String str4 = null, str5 = null;
  162. Object Object1 = null;
  163. Object Object2 = null;
  164.  
  165. if(b == Button1)
  166. {
  167. //The below command line removes the cell editor of the JTable to
  168. //prevent any repitation of data from being added to the JTable
  169.  
  170. Table1.removeEditor();
  171.  
  172. insertcolumn(Table1);
  173. }
  174.  
  175. else if(b == Button2)
  176. {
  177. //The below command line removes the cell editor of the JTable to
  178. //prevent any repitation of data from being added to the JTable
  179.  
  180. Table1.removeEditor();
  181.  
  182. //The below two command lines creates and adds an empty object
  183. //an a row into the current JTable
  184.  
  185. Object[] v = new Object[0];
  186. TableModel1.addRow(v);
  187. }
  188.  
  189. }
  190. public void itemStateChanged(ItemEvent event)
  191. {
  192. JComponent c = (JComponent)event.getSource();
  193. boolean d;
  194.  
  195. if(c == ComboBox1)
  196. {
  197. Table1.editCellAt(0,0);
  198. FontFamily = (String)ComboBox1.getSelectedItem();
  199. TextPane1 = (JTextPane)Table1.getEditorComponent();
  200.  
  201. if(TextPane1 != null)
  202. {
  203. StyleConstants.setFontFamily(sas, FontFamily);
  204. setAttributeSet(sas);
  205. }
  206.  
  207. }
  208.  
  209. }
  210.  
  211. public static void main(String args[])
  212. {
  213. TabTest a = new TabTest();
  214. a.initialize();
  215. }
  216. }
  217.  
  218. class CellPaneRenderer extends JTextPane implements TableCellRenderer
  219. {
  220.  
  221.  
  222. public CellPaneRenderer()
  223. {
  224.  
  225. }
  226.  
  227. public Component getTableCellRendererComponent(JTable table, Object value,
  228. boolean isSelected, boolean hasFocus,
  229. int row, int column)
  230. {
  231.  
  232. setText((String)value);
  233. setSize(table.getColumnModel().getColumn(column).getWidth(), getPreferredSize().height);
  234.  
  235. if(table.getRowHeight(row) != getPreferredSize().height)
  236. {
  237. table.setRowHeight(row, getPreferredSize().height);
  238. }
  239.  
  240. return this;
  241.  
  242. }
  243.  
  244. }

Why this exception is occurring i am not very sure and really hope someone can help me with this problem.

Any help is greatly appreciated

Thank You

Yours Sincerely

Richard West
Similar Threads
Reputation Points: 25
Solved Threads: 10
Practically a Master Poster
freesoft_2000 is offline Offline
623 posts
since Jun 2004
Nov 21st, 2006
0

Re: JTable Cell Renderers

Found your problem.

Quote ...
although i am using a JTextPane as a cell renderer.
Correct, but you haven't set the default editor, so its still editing a JTextField, just rendering it as a JTextPane.

Add this:

Java Syntax (Toggle Plain Text)
  1. Table1.setDefaultEditor(Object.class, new CellPaneEditor());
  2.  
  3.  
  4.  
  5. class CellPaneEditor extends DefaultCellEditor
  6. {
  7.  
  8. JTextPane textPane = new JTextPane();
  9.  
  10. public CellPaneEditor()
  11. {
  12. super(new JTextField());
  13. }
  14.  
  15. public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column)
  16. {
  17. return textPane;
  18. }
  19.  
  20. public Component getComponent()
  21. {
  22. return textPane;
  23. }
  24.  
  25. public Object getCellEditorValue()
  26. {
  27. return textPane.getText();
  28. }
  29. }

And just change whatever you need to in the class before you return the components.


Code technically still doesn't work right (font doesn't change and clicking on a new cell auto-inputs text from previous cell), but at least your Exception is gone.
Last edited by Phaelax; Nov 21st, 2006 at 11:07 pm.
Reputation Points: 92
Solved Threads: 51
Practically a Posting Shark
Phaelax is offline Offline
856 posts
since Mar 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Java rmi
Next Thread in Java Forum Timeline: updating a JList





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC