-
CheckBox in JTable
Hello guys, I have a jtable and I want to add check box in the first column so that I can give the user the option to select some of the rows he wants.
in the table I add this code to render the cell from the String value to boolean value but when I want to select some of the rows it displays back the string value.
the code is
Code:
table.getColumnModel().getColumn(0).setCellRenderer(new TableCellRenderer() {
// the method gives the component like whome the cell must be rendered
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,boolean isFocused, int row, int col) {
boolean marked = (Boolean) value;
JCheckBox rendererComponent = new JCheckBox();
if (marked) {
rendererComponent.setSelected(true);
}
return rendererComponent;
}
});
can you help me in what to change so that I can do what I want. I am newbie in programming. I know that I have to create an array of checkboxes but don't know how to integrate them in this code. any help will be much appreciated!!
regards,
nick
-
Don't set the cell renderer to do this but instead use a custom table model (I usually extend the DefaultTableModel) and in your getColumnClass override method return Boolean.class for the columns where you want a check box. For details on this and sample code, please see the Sun JTable tutorial: Sun Java JTable Tutorial
Oh, I added code tags to your posted code above to improve readability. If you'd like to do this in the future, just highlight your code and press the "CODE" button.
Best of luck and hope this helps.