JTable CheckBox无法使用

时间:2013-09-02 18:24:17

标签: java swing jtable jcheckbox abstracttablemodel

我已经制作了AbstractTableModel,但表格中的复选框不可编辑。当我点击它时,没有任何变化,“Done”列中的复选框仍未选中。我怎样才能让它可以检查?另外,我需要在选中CheckBox时保存订单号,但我不知道该怎么做......

这是我桌子的照片

这是我的TableModel代码:

    public class KitchenTableModel extends AbstractTableModel {

    private ArrayList<WrapperKitchen> hrana;

    public KitchenTableModel(ArrayList<WrapperKitchen> hrana2) {
        this.hrana = hrana2;    
    }

    @Override
    public int getColumnCount() {
        return 8;
    }

    @Override
    public int getRowCount() {
        return hrana.size();
    }

    public String getColumnName(int columnIndex) {
        switch (columnIndex) {
        case 0:return "Order number";
        case 1:return "Room";
        case 2:return "Category";
        case 3:return "Meal";
        case 4:return "Quantity";
        case 5:return "Note";
        case 6:return "Order time";
        case 7:return "Done";
        }
        return null;
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        WrapperKitchen jelo = hrana.get(rowIndex);
        switch (columnIndex) {
        case 0:return jelo.getIdUslugaHrana();
        case 1:return jelo.getBrojSobe();
        case 2:return jelo.getNazivKategorija();
        case 3:return jelo.getNazivHrane();
        case 4:return jelo.getKolicina();
        case 5:return jelo.getNapomena();
        case 6:return jelo.getDatumVrijeme();
        case 7:return jelo.getIzvrseno();
        }
        return null;
    }

    @Override
    public Class<?> getColumnClass(int columnIndex) {
        if (columnIndex == 7)
            return Boolean.class;
        return super.getColumnClass(columnIndex);
    }

    @Override
    public boolean isCellEditable(int rowIndex, int colIndex) {
        return (colIndex == 7);
    }    
}

2 个答案:

答案 0 :(得分:4)

setValueAt()中的AbstractTableModel方法为空。您的实施必须更新您的内部数据结构。

附录:我从未使用过表格。

在这个完整的example中,表模型包含List<Boolean>作为内部数据结构。

答案 1 :(得分:4)

您必须覆盖setValueAt()中的AbstractTableModel,因为默认实施为空。

一个例子:

    @Override
    public void setValueAt(Object inValue, int inRow, int inCol) {
        if(inRow < 0 || inCol < 0 || inRow >= getRowCount() )
            return;

        WrapperKitchen jelo= hrana.get(inRow);
            switch (inCol) {
                case 0:jelo.setIdUslugaHrana((properCast)inValue);break;
                case 1:jelo.setBrojSobe((properCast)inValue);break;
                case 2:jelo.setNazivKategorija((properCast)inValue);break;
                case 3:jelo.setNazivHrane((properCast)inValue);break;
                case 4:jelo.setKolicina((properCast)inValue);break;
                case 5:jelo.setNapomena((properCast)inValue);break;
                case 6:jelo.setDatumVrijeme((properCast)inValue);break;
                case 7:jelo.setIzvrseno((properCast)inValue);break;
                default: throw new RuntimeException("something bad happen incorrect column " + inCol);
            }

        }
        fireTableCellUpdated(inRow, inCol);


    }