Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to bind booleans in JTable with JGoodies

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.

like image 897
smuggledPancakes Avatar asked Nov 13 '22 00:11

smuggledPancakes


1 Answers

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.

like image 107
Denis Zaikin Avatar answered Dec 10 '22 09:12

Denis Zaikin