How to associate Boolean in JTable with JGoodies

I have seven values booleanin a column JTablethat I want to bind to a bean.

How do I tie them together?

All of the binding examples JTablefocus on table selection bindings , but I don't care what the values ​​of these are booleans.

+5
source share
1 answer

You need to implement your own data model. I give you a simplified example that shows the idea of ​​use. Take a look at the 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.

+1
source

All Articles