Reputation: 15351
Up till now I had a definition of the JTable like this:
JTable table = new JTable(model) {
@Override
public Component prepareRenderer(TableCellRenderer renderer, int row, int column) {
Component c = super.prepareRenderer(renderer, row, column);
TradeTableModel model = (TradeTableModel) getModel();
if ((Boolean) model.getValueAt(row, model.findColumn("Select"))) {
Side s = (Side) model.getValueAt(row, model.findColumn("Side"));
if (s == Side.BUY)
c.setBackground(Color.BLUE);
else
c.setBackground(Color.red);
}
else {
c.setBackground(Color.white);
}
return c;
}
};
This was to make sure the rows will change color based on selecting the boolean column value. At my AbstractTableModel
I specified set value method as follows:
public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
assert columnIndex == 5;
try{
Selectable t = trades.get(rowIndex);
t.setSelected((Boolean)aValue);
fireTableDataChanged();
//fireTableCellUpdated(rowIndex, columnIndex);
}
catch(Exception e){
throw new IllegalArgumentException("Object to set was not subtype of boolean");
}
}
If I use fireTableDataChanged()
the color is updated as I click the checkbox on the gui. Howver, I really want to send the fireTableCellUpdated(rowIndex, columnIndex)
as other handlers need to know the location of the cell. However, in this scenario, the row only changes if I click on other row in the table, as if it was delayed and waited for some other event to happen.
ANy ideas why that is the case?
Upvotes: 3
Views: 1286
Reputation: 205835
Your (unseen) TableModel
should fireTableXxxXxxx()
as required in order to notify all listeners. DefaultTableModel
does this automatically; AbstractTableModel
should do so in setValueAt()
. One such listener is the table itself. If "other handlers need to know the location of the cell," they can register for TableModelEvent
instances via addTableModelListener()
. They can also listen for User Selections as needed.
Upvotes: 2
Reputation: 109823
have to notify proper method, fireTableCellUpdated(row, col);
more about AbstractTableModel and prepareRenderer and its methods, please read comment by @camickr
have look at convertXxxToModel
in the case that JTable
is filtered or sorted, e.i.
Upvotes: 2