Search code examples
javaswingjtablejcheckbox

How to set JCheckbox in JTable edditable?


I have JTable for displaying values from the user input. Using a class 'Employee' i take values from the user and display them within the table. I have used the methods getColumnClass(), setValueAt() and isCellEdditable. The result from my program displays the checkbox but does not allow me to tick the box: enter image description here

public class ETableModel extends AbstractTableModel {

    private boolean checked;

    private List<Employee> eDb;

    private String[] colNames = {"Select", "Name", "National Insurance Number", "National Insurance Catagory",
            "Contact number", "Email", "Address", "Town/City", "Postcode"};

    public String getColumnName(int column) {
        return colNames[column];
    }

    public ETableModel(){
    }

    public boolean isCellEdditable(int row, int col){
        switch(col){
        case 0:
            return true;
        default:
            return false;
        }
    }

    public void setData(List<Employee> eDB){
        this.eDb = eDB;
    }

    @Override
    public int getColumnCount() {
        return colNames.length;
    }

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

    @Override
    public Object getValueAt(int row, int col) {
        Employee employee = eDb.get(row);

        switch(col){
        case 0:
            return checked;
        case 1:
            return employee.getName();
        case 2:
            return employee.getnINumber();
        case 3:
            return employee.getnICatagory();
        case 4:
            return employee.getMobileNum();
        case 5:
            return employee.getEmail();
        case 6:
            return employee.getAddress();
        case 7:
            return employee.getArea();
        case 8:
            return employee.getPostCode();
        }
        return null;
    }

    @Override
    public void setValueAt(Object value, int row, int col){
        switch(col){
        case 0:
            checked = ((Boolean)value);
            fireTableCellUpdated(row, col);
        default:
            return;
        }
    }

    @Override
    public Class<?> getColumnClass(int col) {
        switch(col){
        case 0:
            return Boolean.class;
        case 1:
            return String.class;
        case 2:
            return String.class;
        case 3:
            return NICatagory.class;
        case 4:
            return Integer.class;
        case 5:
            return String.class;
        case 6:
            return String.class;
        case 7:
            return String.class;
        case 8:
            return String.class;
        default:
            return null;
        }
    }

}

Does anyone have any pointers? I'm guessing the issue is within my setValueAt() method but I am not sure how to correct this. Most example are using:

Data[int row][int col] = ((Boolean) value);

However, I am not sure how to use this when my values are being taken from user input.


Solution

  • I'm guessing the issue is within my setValueAt() method

    Yes, in that method you need to update member eDB in class ETableModel.

    Also, you spelled isCellEditable wrong. Is this a typo? If it is not then you need to correct your code.

    Good Luck!