如何检查JTable中的多个复选框?

时间:2015-05-26 21:39:34

标签: java mysql swing jtable jcheckbox

所以我有这张桌子:

table = new JTable();

为我的模型建模:table.setModel(model);

内部我有一个名为 CheckColumn 的列,它只有 CheckBoxes

问题是,如果我选中一个框,它会检查它,如果我想检查另一个框,它会删除之前的检查。所以它让我只勾选一个复选框。

对于我正在使用以下内容的复选框:

table.getColumnModel().getColumn(15).setCellEditor(new CheckBoxCellEditor());
table.getColumnModel().getColumn(15).setCellRenderer(new CWCheckBoxRenderer());

来自班级:

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());

        Component c = table.getDefaultRenderer(String.class).getTableCellRendererComponent(table, value, isSelected, false, row, column);
        if (c != null) {
            checkBox.setBackground(c.getBackground());
        }

        return checkBox;
    }
    public Object getCellEditorValue() {
        return Boolean.valueOf(checkBox.isSelected());
    }
}

class CWCheckBoxRenderer extends JCheckBox implements TableCellRenderer {

    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 {
            setSelected(true);
            setEnabled(true);
            return null;
        }

        return this;
    }

}

我希望能够检查多行。有人能告诉我我该怎么办?是否需要在代码中修改某些内容,或者我需要一种新的方法?

这是模型的总结:

    private static final int CheckCol = 15;
    private List<Clients> clients;

    public boolean isCellEditable(int row, int col){
             if(col == 15){
                 return true;

             }
             return false;
         }


    public void setValueAt(Object value, int row, int col) {
            //if(col==15) (not sure what to do in here)

            //fireTableCellUpdated(row, col);
        }

    public Object getValueAt(int row, int col) {
            Clients tempClient = clients.get(row);

            switch (col) {

            case CheckCol:
                return false;  //my CheckColumn type is Boolean


            default:
                return false;

            }
}

我已经尝试了一切我能想到的改变模型......有人能告诉我该模型应该是什么样的?我需要从mysql服务器获取数据,我不知道如何建模复选框列

提前致谢!

0 个答案:

没有答案