I have a table model containing elements of type JCheckBox. I want the content of this table to be different according to the value of a JComboBox.
My problem is the following : if I toggle a few checkboxes and then change the value of my combo box, all the check boxes take the default value (this is what I want, because the boolean values are those of the selected item in the JCheckBox) except the last one I toggled before changing the value of the combo box.
And here is how I implemented this :
public class ValsSelectionTableModel extends MyAbstractTableModel {
private final JComboBox<Data> dataField;
private final Map<Data, JCheckBox[][]> modifiedVals = new HashMap<>();
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
Data data = (Data) dataField.getSelectedItem();
if (!modifiedVals.containsKey(data))
modifiedVals.put(data,
buildCheckBoxesFrom(ClassWithStaticFields.defaultBoolArray));
return modifiedVals.get(data)[rowIndex][columnIndex];
}
private JCheckBox[][] buildCheckBoxesFrom(boolean[][] boolArray) {
JCheckBox[][] checkBoxArray =
new JCheckBox[boolArray.length][boolArray[0].length];
for (int i = 0 ; i < checkBoxArray.length ; i++)
for (int j = 0 ; j < checkBoxArray[i].length ; j++) {
checkBoxArray[i][j] = new JCheckBox();
checkBoxArray[i][j].setSelected(boolArray[i][j]);
checkBoxArray[i][j].setHorizontalAlignment(SwingConstants.CENTER);
}
return checkBoxArray;
}
}
Has anyone got an idea what's wrong with this ?
EDIT : I forgot something important (otherwise the JComboBox selection would not change the display) : I added this actionListener to my JComboBox :
public class MyListener implements ActionListener {
private final ValsSelectionTableModel tableModel;
public MyListener(ValsSelectionTableModel tableModel) {
this.tableModel = tableModel;
}
@Override
public void actionPerformed(ActionEvent e) {
tableModel.fireTableDataChanged();
}
}