
how to insert check box into jtable row in swing

import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.table.*;
public class CheckBoxInJTable extends JPanel {
public CheckBoxInJTable() {
Object[] columns = new Object[] {"Select", "Name"};
Object[][] data = new Object[2][2];
data[0][0] = new Boolean(false);
data[0][1] = "John";
data[1][0] = new Boolean(false);
data[1][1] = "Bill";
MyTableModel model = new MyTableModel(data, columns);
JTable table = new JTable(model);
table.getColumnModel().getColumn(0).setCellEditor(new CheckBoxCellEditor());
table.getColumnModel().getColumn(0).setCellRenderer(new CWCheckBoxRenderer());
JScrollPane tableScroller = new JScrollPane(table);
add(tableScroller);
}
private class MyTableModel extends AbstractTableModel {
private Object[][] data;
private Object[] columns;
public MyTableModel(Object[][] data, Object[] columns) {
this.data = data;
this.columns = columns;
}
public Class getColumnClass(int columnIndex) {
return data[0][columnIndex].getClass();
}
public int getColumnCount() {
return columns.length;
}
public int getRowCount() {
return data.length;
}
public Object getValueAt(int rowIndex, int columnIndex) {
return data[rowIndex][columnIndex];
}
public boolean isCellEditable(int rowIndex, int columnIndex) {
return (columnIndex == 0 || columnIndex == 2);
}
}
private class CheckBoxCellEditor extends AbstractCellEditor implements TableCellEditor {
protected JCheckBox checkBox;
public CheckBoxCellEditor() {
checkBox = new JCheckBox();
checkBox.setHorizontalAlignment(SwingConstants.CENTER);
checkBox.setBackground( Color.white);
}
public Component getTableCellEditorComponent(
JTable table,
Object value,
boolean isSelected,
int row,
int column) {
checkBox.setSelected(((Boolean) value).booleanValue());
return checkBox;
}
public Object getCellEditorValue() {
return Boolean.valueOf(checkBox.isSelected());
}
}
private class CWCheckBoxRenderer extends JCheckBox implements TableCellRenderer {
Border border = new EmptyBorder(1,2,1,2);
public CWCheckBoxRenderer() {
super();
setOpaque(true);
setHorizontalAlignment(SwingConstants.CENTER);
}
public Component getTableCellRendererComponent(
JTable table,
Object value,
boolean isSelected,
boolean hasFocus,
int row,
int column) {
if (value instanceof Boolean) {
setSelected(((Boolean)value).booleanValue());
setEnabled(table.isCellEditable(row, column));
if (isSelected) {
setBackground(table.getSelectionBackground());
setForeground(table.getSelectionForeground());
} else {
setForeground(table.getForeground());
setBackground(table.getBackground());
}
}
else {
return null;
}
return this;
}
}
public static void main(String[] args) {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
CheckBoxInJTable test = new CheckBoxInJTable();
f.getContentPane().add(test);
f.setSize(350, 350);
f.show();
}
}
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.