Reputation: 65
I have a JTable with few columns that are painted as checkboxes. What I want to do is to enable/disable checkbox from Column A by checking/unchecking corresponding checkbox in Column B.
I have managed to do it basing on this example, but I have a problem with newly enabled/disabled checkboxes - they are not refreshing properly. Checkbox which was enabled/disabled last is refreshed only after I click on any other cell in the table.
Problem looks like this ("Ref. structure" column is the one with enabled/disabled checkboxes):
Checkbox not enabled:
Checkbox not disabled:
This is my JTable code:
public class StructuresJTable extends JTable {
public StructuresJTable() {
super();
}
public StructuresJTable(TableModel dm) {
super(dm);
}
@Override
public Component prepareRenderer(TableCellRenderer renderer, int row,
int col) {
Component c = super.prepareRenderer(renderer, row, col);
if (col == StructuresTableModel.COMPARISON_REF_STRUCT_COL) {
c.setEnabled((Boolean) this.dataModel.getValueAt(row,
StructuresTableModel.COMPARE_COL));
} else {
c.setEnabled(true);
}
return c;
}
}
Do you have any suggestions how to make it work?
Upvotes: 2
Views: 4011
Reputation: 9875
I haven't looked at your code, but in general with Swing tables, lists and trees, the renderer instance is reused to draw many rows. You need to tell the JTable that its contents has changed, so that it may redraw the relevant rows. Use the model to notify its TableModelListeners that some rows have changed.
Upvotes: 2
Reputation: 205785
Update: Using prepareRenderer()
versus implementing TableCellRenderer
isn't so important as conditioning the model correctly. If the model is correct, the view will follow. You'll need a suitable model, renderer and editor.
Model: You'll need some data structure to hold the state of each cell. In your implementation of TableModel
, override setValueAt()
to condition the desired state of each cell in the model as changes occur.
Renderer: In your TableCellRenderer
, override getTableCellRendererComponent()
to condition the renderer for each cell as specified in the model.
Editor: The DefaultCellEditor
, used here implicitly, should be sufficient.
Upvotes: 3