Java JTable复选框不可编辑

Java JTable复选框不可编辑,java,swing,jtable,jcheckbox,abstracttablemodel,Java,Swing,Jtable,Jcheckbox,Abstracttablemodel,我已经创建了我的AbstractTableModel,但我的表格中的复选框不可编辑。当我点击它时,没有任何变化,我在“完成”列中的复选框仍处于未选中状态。我如何使它可检查?此外,我需要保存订单号时,复选框是选中的,但我不知道怎么做 这是我桌子的照片 以下是我的TableModel代码: public class KitchenTableModel extends AbstractTableModel { private ArrayList<WrapperKitchen&

我已经创建了我的
AbstractTableModel
,但我的表格中的复选框不可编辑。当我点击它时,没有任何变化,我在“完成”列中的复选框仍处于未选中状态。我如何使它可检查?此外,我需要保存订单号时,复选框是选中的,但我不知道怎么做

这是我桌子的照片

以下是我的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);
    }    
}
公共类KitchenTableModel扩展了AbstractTableModel{
私人ArrayList hrana;
公共厨房表模型(ArrayList hrana2){
this.hrana=hrana2;
}
@凌驾
public int getColumnCount(){
返回8;
}
@凌驾
public int getRowCount(){
返回hrana.size();
}
公共字符串getColumnName(int columnIndex){
开关(列索引){
案例0:返回“订单号”;
案例1:返回“房间”;
案例2:返回“类别”;
案例3:返回“膳食”;
案例4:返回“数量”;
案例5:返回“注释”;
案例6:返回“订单时间”;
案例7:返回“完成”;
}
返回null;
}
@凌驾
公共对象getValueAt(int行索引、int列索引){
WrapperKitchen jelo=hrana.get(rowIndex);
开关(列索引){
案例0:返回jelo.getIdUslugaHrana();
案例1:返回jelo.getBrojSobe();
案例2:返回jelo.getNazivKategorija();
案例3:返回jelo.GetNazivhran();
案例4:返回jelo.getKolicina();
案例5:返回jelo.getNapomena();
案例6:返回jelo.getDatumVrijeme();
案例7:返回jelo.getIzvrseno();
}
返回null;
}
@凌驾
公共类getColumnClass(int columnIndex){
如果(columnIndex==7)
返回Boolean.class;
返回super.getColumnClass(columnIndex);
}
@凌驾
公共布尔值isCellEditable(int rowIndex,int colIndex){
返回值(colIndex==7);
}    
}
AbstractTableModel中的
setValueAt()
方法为空。您的实现必须更新内部数据结构

增编:我从未使用过表格

在本部分中,表格模型包含一个
列表
作为内部数据结构。

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

增编:我从未使用过表格


在本部分中,表模型包含一个
列表
作为内部数据结构。

您必须在
AbstractTableModel中重写
setValueAt()
,因为默认实现为空

例如:

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


    }
@覆盖
public void setValueAt(对象无效、当前输入、输入输入){
如果(inRow<0 | | inCol<0 | | inRow>=getRowCount())
返回;
包装厨房jelo=hrana.get(inRow);
开关(inCol){
案例0:jelo.setIdUslugaHrana((财产)无效);破产;
案例1:jelo.setBrojSobe((财产)无效);中断;
案例2:jelo.setNazivKategorija((财产)无效);破产;
案例3:jelo.Setnazivhran((财产)无效);破产;
案例4:jelo.setKolicina((财产)无效);破产;
案例5:jelo.setNapomena((财产)无效);破裂;
案例6:jelo.setDatumVrijeme((财产)无效);破产;
案例7:jelo.setIzvrseno((财产)无效);破产;
默认值:抛出新的RuntimeException(“不正确的错误发生列”+inCol);
}
}
FireTableCell更新(inRow,inCol);
}

您必须重写
AbstractTableModel
中的
setValueAt()
,因为默认实现为空

例如:

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


    }
@覆盖
public void setValueAt(对象无效、当前输入、输入输入){
如果(inRow<0 | | inCol<0 | | inRow>=getRowCount())
返回;
包装厨房jelo=hrana.get(inRow);
开关(inCol){
案例0:jelo.setIdUslugaHrana((财产)无效);破产;
案例1:jelo.setBrojSobe((财产)无效);中断;
案例2:jelo.setNazivKategorija((财产)无效);破产;
案例3:jelo.Setnazivhran((财产)无效);破产;
案例4:jelo.setKolicina((财产)无效);破产;
案例5:jelo.setNapomena((财产)无效);破裂;
案例6:jelo.setDatumVrijeme((财产)无效);破产;
案例7:jelo.setIzvrseno((财产)无效);破产;
默认值:抛出新的RuntimeException(“不正确的错误发生列”+inCol);
}
}
FireTableCell更新(inRow,inCol);
}

make help如果您在同一时间发布make help:D+1在同一时间:D+1我是Java新手,我从未使用过表。当我将您的代码复制到我的代码中时,编译器会在调用fireTableCellUpdated()方法(方法的返回类型丢失)的行中以及开关大小写的每一行中显示错误。在switch case中存在错误,因为我必须声明“jelo”对象,但我不知道如何声明,因为我在这个方法中没有rowIndex。。。帮助please@MahirDuraković现在我是Java新手,我从未使用过表。当我将您的代码复制到我的代码中时,编译器会在调用fireTableCellUpdated()方法(方法的返回类型丢失)的行中以及开关大小写的每一行中显示错误。在switch case中存在错误,因为我必须声明“jelo”对象,但我不知道如何声明,因为我在这个方法中没有rowIndex。。。帮助please@MahirDurakovi现在见