Default checkbox renderer in JTabel
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Toolkit;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
public class CheckBoxCellinTable extends JFrame {
JTable table;
public CheckBoxCellinTable() {
this.setLayout(new BorderLayout());
this.initTable();
setSize(400, 300);
Dimension sc = Toolkit.getDefaultToolkit().getScreenSize();
setLocation((sc.width - getWidth()) / 2, (sc.height - getHeight()) / 2);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
void initTable() {
Object[] columnNames = { "First Name", "Last Name", "Married" };
Object[][] data = { { "Harry", "Smith", true }, { "Sally", "Jones", false }, { "Bob", "Clark", true }, { "Eric", "Burke", true } };
DefaultTableModel model = new DefaultTableModel(data, columnNames) {
@Override
public boolean isCellEditable(int row, int column) {
return column == 2;
}
@Override
public Class<?> getColumnClass(int columnIndex) {
return columnIndex == 2 ? Boolean.class : super.getColumnClass(columnIndex);
}
};
table = new JTable(model);
this.add(new JScrollPane(table), BorderLayout.CENTER);
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
new CheckBoxCellinTable();
}
}