How to make Jtable column contain checkboxes?
Posted
by theraven
on Stack Overflow
See other posts from Stack Overflow
or by theraven
Published on 2010-05-25T01:40:32Z
Indexed on
2010/05/25
1:51 UTC
Read the original article
Hit count: 348
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 :)
© Stack Overflow or respective owner