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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With