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.
Originally posted by Richard West: I tried writing and using the below code by extending the DefaultCellEditor class and uisng it but it only got worse as now if i apply the font to the selected text in the cell all the text dissapears
Here is the code of the class theat extends the default cell editor class
Well of course the text disappears, since the JTextPane you return in your getTableCellEditorComponent() has never had any text added to it.
You can try something like textPane1.setText((String)value). Or since you are keeping a reference to the Document you could do something with dse.replace() instead, though I'm not sure why you need the 'dse' field. I'm also not sure why you are messing with StyledEditorKits.
I would also recommend that you change the textPane1 and styleEditorKit1 fields to not begin with capital letters, though that makes no real difference.
I decided to revamp the entire cell editor and the new editor looks like the below
There now still seems to be a little problem.
You see now font can be applied to the cell in a particular sentence when the cell is selected but when the cell is deselected the font all dissapears
Also once i have have applied font to the selected text in the and deselected the cell the font all dissapears as mentioned earlier but also when i edit the same cell again the font is also all dissapears.
Why is it reacting in this way? Am i missing something?
Will it make a difference if a renderer that is appropriate for this use is written?
If yes how does one go about writing this renderer?
DefaultCellEditor is a convenience class for three very specific components. You are using a different component so this class will not work. When you super(new JTextField()) in the constructor, the editor will use a JTextField. It will not understand anything about JTextPane. When doing custom rendering and editing the renderer and editor must be matching components, ie, built the same. The table is a drawing, a graphic snapshot made by configuring the renderer, translating it to the (row, column) location and having it draw itself. The table and its model have only the [Object value] to use to give to the renderer for the rendering. There is no provision made to store these attribute–value pairs in this object. Using this [Object value] is the only way we have of saving, and transferring information to be used to make the table. So it would make sense to create a class that can store the information you want to render in your table, both text and attributes for the StyledDocument. There are many ways to put these things together. Here's a minimal modification of your app to help you get started: