| | |
JTable Cell Renderers
![]() |
•
•
Join Date: Jun 2004
Posts: 609
Reputation:
Solved Threads: 7
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
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
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)
import java.awt.*; import java.awt.image.*; import java.io.*; import java.text.*; import java.awt.event.*; import java.awt.geom.*; import javax.swing.*; import javax.swing.event.*; import javax.swing.table.*; import javax.swing.text.*; public class TabTest implements ActionListener, ItemListener { JFrame fr = new JFrame ("Frame"); JButton Button1 = new JButton("Add Coloum"); JButton Button2 = new JButton("Add Row"); JComboBox ComboBox1; DefaultTableModel TableModel1 = new DefaultTableModel(0, 0); JTable Table1 = new JTable(TableModel1); JScrollPane ScrollPane1 = new JScrollPane(Table1, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS); String FontFamily = "Arial"; Dimension Size1 = new Dimension(); //add //The below command line is the constructor for the JTextPane JTextPane TextPane1 = new JTextPane(); //The below two command lines creates instances for fonts SimpleAttributeSet sas = new SimpleAttributeSet(); StyleContext sc = new StyleContext(); //The below command line sets up the variable for font updating MutableAttributeSet mas; //The below command line is the default document class which //has one argument as explained below //The first argument sets the Style Context of the styled document DefaultStyledDocument dse = new DefaultStyledDocument(sc); StyledEditorKit StyledEditorKit1 = new StyledEditorKit(); CellPaneRenderer CellPaneRenderer1 = new CellPaneRenderer(); //end 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); Table1.setGridColor(Color.black); Size1.width = 350; Size1.height = 250; ScrollPane1.setPreferredSize(Size1); Table1.setModel(TableModel1); Table1.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); Table1.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); Table1.setDefaultRenderer(Object.class, new CustomTableCellRenderer(Color.white)); Table1.setDefaultRenderer(Object.class, new CellPaneRenderer()); pane.add(ScrollPane1); pane.add(Button1); pane.add(Button2); combofontfamilyinitialize(); pane.add(ComboBox1); fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Button1.addActionListener(this); Button2.addActionListener(this); ComboBox1.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 setAttributeSet(AttributeSet attr) { //This function only set the specified font set by the //attr variable to the text selected by the mouse int xStart, xFinish, k; xStart = TextPane1.getSelectionStart(); xFinish = TextPane1.getSelectionEnd(); k = xFinish - xStart; if(xStart != xFinish) { dse.setCharacterAttributes(xStart, k, attr, false); } else if(xStart == xFinish) { //The below two command line updates the JTextPane according to what //font that is being selected at a particular moment mas = StyledEditorKit1.getInputAttributes(); mas.addAttributes(attr); } } public void insertcolumn (JTable table2) { //This function adds a column dynamically to the end of the JTable TableModel1 = (DefaultTableModel)table2.getModel(); TableColumn col = new TableColumn(TableModel1.getColumnCount()); //add col.setCellRenderer(CellPaneRenderer1); //end TableModel1.addColumn(" "); //The below command line adds the new column to the JTable table2.addColumn(col); TableModel1.fireTableStructureChanged(); } public void actionPerformed(ActionEvent event) { JComponent b = (JComponent)event.getSource(); int d; String str3 = null; String str4 = null, str5 = null; Object Object1 = null; Object Object2 = null; if(b == Button1) { //The below command line removes the cell editor of the JTable to //prevent any repitation of data from being added to the JTable Table1.removeEditor(); insertcolumn(Table1); } else if(b == Button2) { //The below command line removes the cell editor of the JTable to //prevent any repitation of data from being added to the JTable Table1.removeEditor(); //The below two command lines creates and adds an empty object //an a row into the current JTable Object[] v = new Object[0]; TableModel1.addRow(v); } } public void itemStateChanged(ItemEvent event) { JComponent c = (JComponent)event.getSource(); boolean d; if(c == ComboBox1) { Table1.editCellAt(0,0); FontFamily = (String)ComboBox1.getSelectedItem(); TextPane1 = (JTextPane)Table1.getEditorComponent(); if(TextPane1 != null) { StyleConstants.setFontFamily(sas, FontFamily); setAttributeSet(sas); } } } public static void main(String args[]) { TabTest a = new TabTest(); a.initialize(); } } class CellPaneRenderer extends JTextPane implements TableCellRenderer { public CellPaneRenderer() { } public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { setText((String)value); setSize(table.getColumnModel().getColumn(column).getWidth(), getPreferredSize().height); if(table.getRowHeight(row) != getPreferredSize().height) { table.setRowHeight(row, getPreferredSize().height); } return this; } }
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
Microsoft uses "One World, One Web, One Program" as a slogan.
Doesn’t that sound like "Ein Volk, Ein Reich, Ein Führer" to you, too?
— Eric S. Raymond
Tell me what type of software do you like and what would you pay for it
http://www.daniweb.com/techtalkforums/thread19660.html
Doesn’t that sound like "Ein Volk, Ein Reich, Ein Führer" to you, too?
— Eric S. Raymond
Tell me what type of software do you like and what would you pay for it
http://www.daniweb.com/techtalkforums/thread19660.html
•
•
Join Date: Mar 2004
Posts: 763
Reputation:
Solved Threads: 38
Found your problem.
Correct, but you haven't set the default editor, so its still editing a JTextField, just rendering it as a JTextPane.
Add this:
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.
•
•
•
•
although i am using a JTextPane as a cell renderer.
Add this:
Java Syntax (Toggle Plain Text)
Table1.setDefaultEditor(Object.class, new CellPaneEditor()); class CellPaneEditor extends DefaultCellEditor { JTextPane textPane = new JTextPane(); public CellPaneEditor() { super(new JTextField()); } public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) { return textPane; } public Component getComponent() { return textPane; } public Object getCellEditorValue() { return textPane.getText(); } }
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.
![]() |
Similar Threads
- How to make JTable cell not Editable (Java)
- new to Swing, JTable questions (Java)
- How can i make perticular row or perticular cell of a JTable as Editable dynamically (Java)
- Adding and deleting a column in JTable (Java)
- JTable Limitation or Not? (Java)
- Clipboards (Java)
- Saving and opeing a JTable (Java)
Other Threads in the Java Forum
- Previous Thread: Java rmi
- Next Thread: updating a JList
| Thread Tools | Search this Thread |
android api applet application apps array arrays automation awt bidirectional binary birt bluetooth businessintelligence busy_handler(null) card chat class classes client code collision columns component constructor crashcourse database designadrawingapplicationusingjavajslider draw eclipse error errors eventlistener exception expand fractal game givemetehcodez graphics gui guidancer html ide image inetaddress integer intellij j2me java javadoc javafx javamicroeditionuseofmotionsensor javaprojects jme jni jpanel jtree julia linux list loop machine map method methods mobile mobiledevelopmentcreatejar myaggfun netbeans newbie oracle plazmic print problem program programming project radio recursion scanner server set sharepoint smart sms smsspam sort sortedmaps sql string subclass support swing textfield threads tree unlimited utility webservices windows





