Set Jtable / Column Renderer for Boolean

Right now my Boolean values ​​for my JTable display are like JCheckBoxes . This is usually normal, but I would like to display them as an alternate line or image. I can make them display as true / false, but I would like to display them as a check mark (βœ”) if true, and nothing if false. Perhaps an image, but it allows you to create a string first ...

+3
source share
2 answers

Create your own renderer. Extend DefaultTableCellRenderer and add your own code to display whatever you want. It can be a custom icon or if the β€œcheckmark” is a printed symbol, than you can simply set the visualizer text to the corresponding symbol.

Read the JTable API and you will find a link to the Swing tutorial on How to Use Tables, which will provide more information about visualizers.

If you need more help, post an SSCCE , showing the problems encountered when creating the render.

Edit:

The tutorial shows how to add a custom renderer for a given class, but it does not show how to add a custom renderer for a specific column. You will use:

 table.getColumnModel().getColumn(...).setCellRenderer(...); 
+8
source

Example:

 table.setDefaultRenderer(Boolean.class, new BooleanRenderer(true)); 

with BooleanRenderer

 public class BooleanRenderer extends JLabel implements TableCellRenderer { ..... } 
+3
source

Source: https://habr.com/ru/post/1441739/


All Articles