问题
I set the renderer to the checkbox on jtable using following code
Object[] ColumnData = {"Sr No","Ward Name","Total voters","Action"}; Object[][] RawData=null;
// in loop
model.insertRow(x, new Object[]{ key,ward_name_var,total_vot_var,new Object[]{o}}); model.setValueAt(o,x,3); tblWard.setModel(model);
Setchk(tblWard,3,checkbox); // by calling this method which contains following
private void Setchk(JTable jTable1, int i, JCheckBox checkbox) { jTable1.getColumnModel().getColumn(i).setCellRenderer((new CWCheckBoxRenderer())); jTable1.getColumnModel().getColumn(i).setCellEditor(new CheckBoxCellEditor()); }
Blockquote
how can we try it for row to set the checkbox on jtable. thanks in advance.
回答1:
If your data is of type Boolean.class
, the default render will display a checkbox. To change the checkbox in a particular row, you need a corresponding CellEditor
. The default render/editor are used here; custom components are illustrated here.
回答2:
You can simply override the getCellRenderer
method of your JTable
to return the desired renderer for a given row. Example:
JTable table = new JTable() {
TableCellRenderer getCellRenderer(int row, int column) {
if (row == checkBoxRow)
return myCheckBoxRenderer;
else
return super.getCellRenderer(row, column);
}
};
来源:https://stackoverflow.com/questions/9905362/how-to-set-renderer-to-checkbox-on-jtable-for-row