سؤال

I've made my AbstractTableModel but my checkbox in table is not editable. 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...

Here is picture of my table

Here is my code of 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);
    }    
}
هل كانت مفيدة؟

المحلول

You have to override setValueAt() in AbstractTableModel cause default implementation is empty.

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


    }

نصائح أخرى

The setValueAt() method in AbstractTableModel is empty. 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.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top