5

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.

SaintLike
  • 9,119
  • 11
  • 39
  • 69
smuggledPancakes
  • 9,881
  • 20
  • 74
  • 113
  • don't quite understand - those booleans are on different beans (aka: rows)? If so, bind to the bean/s directly ... – kleopatra Aug 15 '12 at 08:40
  • do you mean seven different boolean values in a row or in a column. If its in a row, you can define a bean with seven boolean attribute and bind each attribute to each column.If its column, i am not sure what it means. Basic thing is each row would represent a bean. So seven different boolean values on column means seven different beans . – rahul pasricha Nov 27 '12 at 21:39
  • JTables store values as Object so, regardless of being boolean values you can bind them the same way you bind any other value type – Felype Jun 03 '13 at 15:33

1 Answers1

1

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.

Denis Zaikin
  • 569
  • 4
  • 10