简体   繁体   English

JTable CheckBox无法编辑

[英]JTable CheckBox uneditable

I've made my AbstractTableModel but my checkbox in table is not editable. 我已经制作了AbstractTableModel但是表格中的复选框不可编辑。 When I click on it, nothing changes, my checkbox in column "Done" is still unchecked. 当我单击它时,没有任何改变,我在“完成”列中的复选框仍未选中。 How can I make it checkable ? 我如何使其可检查? Also I need to save Order Number when CheckBox is checked, but I dont know how to do it... 另外,当选中CheckBox时,我需要保存订单号,但是我不知道该怎么做...

Here is picture of my table 这是我桌子的照片

Here is my code of TableModel: 这是我的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);
    }    
}

The setValueAt() method in AbstractTableModel is empty. AbstractTableModelsetValueAt()方法为空。 Your implementation must update your internal data structure. 您的实现必须更新内部数据结构。

Addendum: I have never worked with tables. 附录: 我从未使用过表格。

In this complete example , the table model contains a List<Boolean> as the internal data structure. 在这个完整的示例中 ,表模型包含一个List<Boolean>作为内部数据结构。

You have to override setValueAt() in AbstractTableModel cause default implementation is empty. 您必须在AbstractTableModel重写setValueAt() ,因为默认实现为空。

An example: 一个例子:

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


    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM