这是代码....
String Columnas[] = {"Rol","Asignado"};
modeloTabla= new DefaultTableModel(null,Columnas);
objSAP = new SAPFunctionCall();
try {
rolese = objSAP.listaRoles();
modeloTabla.setNumRows(rolese.size());
for (int i=0; i<rolese.size(); i++){
for(int j=0; j<1; j++){
modeloTabla.setValueAt(((BeanRol)rolese.get(i)).getStrNomRol(), i, j);
modeloTabla.setValueAt(Boolean.FALSE, i, j+1);
}
}
}
catch (Exception e) {
e.printStackTrace();
}
tblRol = new JTable();
tblRol.setModel(modeloTabla);
tblRol.getColumnModel().getColumn(1).setCellEditor(new CheckBoxCellEditor());
tblRol.getColumnModel().getColumn(1).setCellRenderer(new CWCheckBoxRenderer());
现在,当我想捕捉复选框的值时...... 例如,第0行第1列......
Object obj;
Boolean bol;
obj = modeloTabla.getValueAt(0, 1);
if (obj instanceof Boolean) {
bol = (Boolean) obj;
if (bol == true)
System.out.print("SELECTED");
else
System.out.print("NO SELECTED");
}
现在附件I附上了这两个类......
public class CheckBoxCellEditor extends AbstractCellEditor implements TableCellEditor {
private static final long serialVersionUID = 1L;
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());
}
}
public class CWCheckBoxRenderer extends JCheckBox implements TableCellRenderer {
private static final long serialVersionUID = 1L;
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;
}
}
重复不捕捉!
答案 0 :(得分:3)
请阅读有关JTable
Boolean
代表JCheckBox as Renderer and Editor的值
然后没有理由在TableCellRenderer
TableCellEditor
创建JCheckBox
和JTable
如果您希望高亮TableCell
取决于返回Boolean
的{{1}}值,请阅读Using Custom Renderers
教程中的示例和here too