Right now my Boolean
values for my JTable display as JCheckBoxes
. This would normally be fine but I would like to display them as either an alternative String or image.
I can get them to display as true/false but I would like to display them as a checkmark (✔) if true and nothing if false. Possibly an image but lets do a String first...
views:
51answers:
2
+2
A:
Create a custom renderer. Extend the DefaultTableCellRenderer and add your own code to display whatever you want. It could be a custom Icon or if the "checkmark" is a printable character than you can just set the renderer text to the appropriate character.
Read the JTable API and you will find a link to the Swing tutorial on "How to Use Tables" which will give more information about renderers.
If you need more help post your SSCCE showing the problems you are having creating the renderer.
Edit:
The tutorial shows how to add a custom renderer for a given class but it doesn't show how to add a custom renderer for a specific column. You would use:
table.getColumnModel().getColumn(...).setCellRenderer(...);
camickr
2010-05-21 05:07:17
How do I then add this to the column? http://pastebin.com/GzTQXrGU
twodayslate
2010-05-22 03:48:56
See edit above.
camickr
2010-05-22 14:54:54
So when I create `BooleanRenderer(Format formatter)` what do I put for formatter...
twodayslate
2010-05-22 18:22:53
Sorry, I have no idea why you are using a Format. You suggested you might use an Icon for the renderer. Well in this cause you just use the setIcon(...) method to set the Icon. If you want to use a displayable character, then you just use the setText(...) method. The default renderer extends JLabel so you use whatever label method is appropriate for how you want to render the Boolean value.
camickr
2010-05-22 19:14:17
got it. thanks!
twodayslate
2010-05-23 02:47:12
+2
A:
Example:
table.setDefaultRenderer(Boolean.class, new BooleanRenderer(true));
with BooleanRenderer
public class BooleanRenderer extends JLabel implements TableCellRenderer
{
.....
}
Peter
2010-05-21 07:15:30
Please read my suggestion to extend DefaultTableCellRenderer (which was given 2 hours earlier). There are multiple reasons for extending the default renderer. First you get the default behaviour by default such as row highlighting, border highlighting when a cell receives focus. Secondly, the default renderer has been optimized for faster painting.
camickr
2010-05-21 15:58:39