-1

这是代码....

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;
    }
}

重复不捕获!

4

1 回答 1

3
于 2012-05-27T22:59:19.067 回答