Can someone provide me with an example of how to display HTML in a cell of a JTable? I know this can be done for other Swing components but I can't seem to figure it out for JTables.
-
2[What have you tried?](http://mattgemmell.com/2008/12/08/what-have-you-tried/) SO is not a code factory. – Andrew Thompson Jan 09 '12 at 19:20
-
What do you want to display in HTML? The default cell renderer is a `JLabel` so you can do some basic HTML by wrapping the contents in ``. – Paul Jan 09 '12 at 19:20
-
@AndrewThompson, you're saying "how do I do X?" is not a valid question? SO is not a "correct my code" factory either - the FAQ says that "...if your question generally covers a specific programming problem...hen you’re in the right place to ask your question!" **You** may want code with every question but it's not required. – Paul Jan 09 '12 at 19:27
-
@Paul That is a comment I generally reserve for those who claim things like *"I can't seem to figure it out for JTables"* If they can't seem to figure it out, they must logically have tried some things. Which leads me to inquire *"What have you tried?"* So when you challenge *"you're saying "how do I do X?" is not a valid question?"* My first reaction is *"What post/comment were **you** reading?"* – Andrew Thompson Jan 09 '12 at 19:33
3 Answers
Are you remembering to put <html> around the HTML markup as per <html>...</html>? The default string renderer in a JTable uses JLabel, so this should work. Just set the String as the cell value.
- 21,982
- 8
- 55
- 84
How to use HTML in Swing components is described in the Swing tutorial. If you combine this with the JTable tutorial, and more specifically with the part about the Editors and renderers and the custom renderers you should get pretty far.
To summarize all those links: either your TableModel contains HTML formatted String instances and then all will work with the standard renderer. Otherwise you can adjust the renderer to convert your model values to a HTML string and show those in for example a JLabel
- 35,423
- 5
- 46
- 96
Among others, I had this method overridden in my cell renderer label implementation:
@Override
protected void firePropertyChange(String propertyName, Object oldValue, Object newValue)
{
// empty override for performance reasons.
}
Once I removed the override, the HTML rendering started to work.
- 4,747
- 4
- 32
- 67