首先是创建表格
JTable demoTable= new JTable();
DefaultTableModel dtmDemo = (DefaultTableModel) demoTable.getModel();
String[] tableHeads = { "序号","id","jCheckBox"};
dtmDemo.setColumnIdentifiers(tableHeads);
//这里的2指的是第几列,从0开始计数
demoTable.getColumnModel().getColumn(2).setCellEditor(new CheckBoxCellEditor());
demoTable.getColumnModel().getColumn(2).setCellRenderer(new CWCheckBoxRenderer());
//JTable中的数据
for(int i=0;i<10;i++){
Object[] objdata = { i,2,new Boolean(false)/**这里就那个JCheckBox位置*/};
dtmDemo.addRow(objdata);
}
将下面的类容复制粘贴到你正在想加组件的类下面
//~ Inner Classes ----------------------------------------------------------------------------------------------------
class CheckBoxCellEditor extends AbstractCellEditor implements TableCellEditor {
//~ Static fields/initializers -------------------------------------------------------------------------------------
private static final long serialVersionUID = 1L;
//~ Instance fields ------------------------------------------------------------------------------------------------
protected JCheckBox checkBox;
//~ Constructors ---------------------------------------------------------------------------------------------------
public CheckBoxCellEditor() {
checkBox = new JCheckBox();
checkBox.setHorizontalAlignment(SwingConstants.CENTER);
// checkBox.setBackground( Color.white);
}
//~ Methods --------------------------------------------------------------------------------------------------------
@Override public Object getCellEditorValue() {
return Boolean.valueOf(checkBox.isSelected());
}
//~ ----------------------------------------------------------------------------------------------------------------
@Override public Component getTableCellEditorComponent(
JTable table,
Object value,
boolean isSelected,
int row,
int column) {
checkBox.setSelected(((Boolean) value).booleanValue());
return checkBox;
}
} // end class CheckBoxCellEditor
class CWCheckBoxRenderer extends JCheckBox implements TableCellRenderer {
//~ Static fields/initializers -------------------------------------------------------------------------------------
private static final long serialVersionUID = 1L;
//~ Instance fields ------------------------------------------------------------------------------------------------
Border border = new EmptyBorder(1, 2, 1, 2);
//~ Constructors ---------------------------------------------------------------------------------------------------
public CWCheckBoxRenderer() {
super();
setOpaque(true);
setHorizontalAlignment(SwingConstants.CENTER);
}
//~ Methods --------------------------------------------------------------------------------------------------------
@Override 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));
setForeground(table.getForeground());
setBackground(table.getBackground());
}
return this;
}
} // end class CWCheckBoxRenderer
最后效果: