This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes JTable Cell Renderers Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JTable Cell Renderers" Watch "JTable Cell Renderers" New topic
Author

JTable Cell Renderers

Richard West
Ranch Hand

Joined: Jan 07, 2005
Posts: 127
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
Sam Codean
Ranch Hand

Joined: Feb 26, 2006
Posts: 194
Where are you setting the editor component ?? I see that you are only setting a JTextPane as the Cell Renderer and not the Cell Editor. Both are different.


-Sam Codean<br />SCJP 1.4 (98%)<br />SCJD 5.0 (87.5%)
Richard West
Ranch Hand

Joined: Jan 07, 2005
Posts: 127
Hi everyone,

Originally posted by Sam Codean:
Where are you setting the editor component ?? I see that you are only setting a JTextPane as the Cell Renderer and not the Cell Editor. Both are different.


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



Here is the entire code using the above class which compiles



Why the JTable is reacting in this way i am not sure and hope someone can
point out to me why this is happening.

Thank You

Yours Sincerely

Richard West
[ November 21, 2006: Message edited by: Richard West ]
Richard West
Ranch Hand

Joined: Jan 07, 2005
Posts: 127
Hi everyone,

Anyone at all?

Richard West
Brian Cole
Author
Ranch Hand

Joined: Sep 20, 2005
Posts: 862
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.


bitguru blog
Richard West
Ranch Hand

Joined: Jan 07, 2005
Posts: 127
Hi everyone,

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?

Any help is greatly appreciated

Thank You

Yours Sincerely

Richard West
Peter Taucher
Ranch Hand

Joined: Nov 18, 2006
Posts: 174
Head over to:
http://wordhoard.northwestern.edu/userman/javadoc/edu/northwestern/at/utils/swing/XTextPaneTableCellRenderer.html

It's under GNU General Public License and seems to run very well.


Censorship is the younger of two shameful sisters, the older one bears the name inquisition.
-- Johann Nepomuk Nestroy
Richard West
Ranch Hand

Joined: Jan 07, 2005
Posts: 127
Hi everyone,

Originally posted by Peter Taucher:
Head over to:
http://wordhoard.northwestern.edu/userman/javadoc/edu/northwestern/at/utils/swing/XTextPaneTableCellRenderer.html

It's under GNU General Public License and seems to run very well.


The link you showed me leads to a javadoc and does not seem to lead to any examples or code.

Do have the link to where this TextPane renderer code or example resides at?

Richard West
Peter Taucher
Ranch Hand

Joined: Nov 18, 2006
Posts: 174
You coul've come up with this very simple (just go to the homepage where the javadoc is hosted):
http://wordhoard.northwestern.edu/userman/dev-files.html
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
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:
 
 
subject: JTable Cell Renderers