如何使用JCheckBox在JTable中设置多行选择?

时间:2015-02-21 18:54:47

标签: java swing jtable jcheckbox

我有JTable,第一列可编辑为复选框。它适用于单行选择,但如果我想检查多行,我不知道该怎么办。 我的类实现了TableCellRenderer:

private class MyTableRenderer extends JCheckBox implements TableCellRenderer{

    private int row;

    public MyTableRenderer() {
    }

    @Override
    public Component getTableCellRendererComponent(JTable table,
            Object value, boolean isSelected, boolean hasFocus, int row, int col) {

        if(isSelected) {
            this.row = table.getSelectedRow();

            if(isSelected()) setSelected(false);
            else setSelected(true);
        }
        return this;
    }
}

并且有一部分类扩展了AbstractTableModel:

private class CompanyTable extends AbstractTableModel {

    //...

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {

        if(columnIndex==0) {
            MyTableRenderer renderer = new MyTableRenderer();
            TableColumn tableColumn = new TableColumn();
            tableColumn = table.getColumnModel().getColumn(0);
            tableColumn.setCellRenderer(renderer);
            return null;
        }
        if(columnIndex==1)
            //...
        else
            //...
    }

    @Override
    public String getColumnName(int column) {
        // TODO Auto-generated method stub
        return columnsName[column];
    }

    @Override
    public Class<?> getColumnClass(int columnIndex) {
        return super.getColumnClass(columnIndex);
    }

    @Override
    public boolean isCellEditable(int rowIndex, int columnIndex) {
        if(columnIndex == 0) return true;
        return false;
    }

你知道我该怎么办?

0 个答案:

没有答案