How to make JTable column contain checkboxes?
Asked Answered
J

5

8

Preface: I am horrible with java, and worse with java ui components.

I have found several different tutorials on how to add buttons to tables, however I am struggling with adding checkboxes. I need to have a column that draws a text box ticked on default (cell renderer i think handles that), then on click of tickbox, unticks the box, redraws said box, and fires off an event somewhere I can track.

currently I have a custom cellrenderer:

public class GraphButtonCellRenderer extends JCheckBox implements TableCellRenderer {
public GraphButtonCellRenderer() {
}
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
    if(isSelected)
        setSelected(true);
    else
        setSelected(false);
    setMargin(new Insets(0, 16, 0, 0));
    setIconTextGap(0);

    setBackground(new Color(255,255,255,0));
    return this;
}}

Which currently handles drawing the tick box, but only ticks and unticks the box if that row is selected. But I don't know how to handle the events. Really what I am asking is possibly a link to a good tutorial on how to add checkboxes cleanly to a JTable. Any assist is greatly appreciated :)

Judicial answered 25/5, 2010 at 1:40 Comment(0)
C
25

There's no need to create your own table renderer. Here's a simpler example. Just create a custom table model and for a given column return the class Boolean for:

public Class getColumnClass(int column)

If you want the column to be editable, return true for

public boolean isCellEditable(int row, int column)

JTable takes care of the rendering for you.

Another example is here.

Clematis answered 25/5, 2010 at 1:50 Comment(2)
+1 Good point; simpler may suffice. The tutorial describes several default renderers for specific classes.Cocky
Accepted. Thank you very much for the links, these are simple enough for me to follow/integrate with my current projectJudicial
M
6

As Peter say, its easy using extended DefaultTableModel class, ex:

class NewTableModel extends DefaultTableModel{
        public Class<?> getColumnClass(int columnIndex) {
            return getValueAt(0, columnIndex).getClass();
        }
    }
Maness answered 7/8, 2011 at 19:43 Comment(0)
C
4

Here's a simple rather elaborate example using a TableCellRenderer and TableCellEditor. See also, Concepts: Editors and Renderers.

Addendum: @Jay Askren's point is well taken. The default renderer for Boolean.class, as described in the tutorial, may be all you need.

Cocky answered 25/5, 2010 at 1:43 Comment(1)
Thank you. I hadn't come across that particular example. So much stuff going on... I'll give it a crackJudicial
S
3

The easiest solution is to use the DefaultTableModel and use Boolean object as values.

Sher answered 25/5, 2010 at 7:2 Comment(1)
yeah that's true @Peter, But could we enhance more by adding some value (ID) to that checkbox if we want the TRUE value become an ID value, and FALSE still FALSE value?Highboy
M
-5

In the Swing Designer set column type to boolean

Mam answered 11/11, 2012 at 5:54 Comment(1)
this solution assumes the OP is using a UI builder of some typeBilabial

© 2022 - 2024 — McMap. All rights reserved.