1

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);
    }    
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
Mahir Duraković
  • 135
  • 1
  • 23

2 Answers2

4

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.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
4

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


    }
nachokk
  • 14,363
  • 4
  • 24
  • 53
  • I am newbie with Java and I have never worked with tables. When I copy your code in mine,complier show errors in line where you call fireTableCellUpdated() method (Return type of method is missing)and in every line of switch case. Error in switch case is there because I must declare "jelo" object, but I don't know how because I dont have rowIndex in this method... Help please – Mahir Duraković Sep 02 '13 at 18:47
  • @MahirDuraković see now – nachokk Sep 02 '13 at 18:50