Question

I have seven boolean values in a column of a JTable that I want to bind to my bean.

How do I bind them?

All the JTable binding examples out there focus on binding the table selection, but I only care about what the values of those booleans are.

Was it helpful?

Solution

You need to implement your own data model. I give you simplified example that shows idea of usage. Take a look at getColumnClass method.

Usage: table.setModel(new DataModel(myData));

class DataModel extends AbstractTableModel
{


    public DataModel(Object yourData){
         //some code here
    }

    @Override
    public int getRowCount() {
        return yourData.rows;
    }

    @Override
    public int getColumnCount() {
        return yourData.colums;
    }

    @Override
    public Class<?> getColumnClass(int col) {
        if (col == myBooleanColumn) {
            return Boolean.class;
        } else {
            return null;
        }
    }

    @Override
    public boolean isCellEditable(int row, int col) 
    {
        return col >= 0;
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {

        return yourData.get(rowIndex,columnIndex);
    }

    @Override
    public void setValueAt(Object aValue, int row, int col) {           

    yourData.set(aValue,row,col)    

        this.fireTableCellUpdated(row, col);  
    }
}

Hope this helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top