I have set up a JPanel which can be navigated to, away from, and then returned to any number of times.
This JPanel has on it two JRadioButtons and has inside a JScrollPane containing a varying number of JComboBoxes. I have coded it so that when one JRadioButton is selected the JScrollPane and the JComboBoxed are enabled, and when the other is pressed they are disabled using:
private void automaticActionPerformed(java.awt.event.ActionEvent evt) {
enableDisable();
check();
}
private void manualActionPerformed(java.awt.event.ActionEvent evt) {
enableDisable();
check();
}
private void enableDisable() {
boolean manual = manual.isSelected();
JPanel.setEnabled(manual);
for (LabelComboPair pair :labelsAndCombos){
pair.label.setEnabled(manual);
pair.combo.setEnabled(manual);
}
}
public void aboutToDisplayPanel() {
...
...
...
enableDisable();
}
This works works exactly right the first time I navigate to the screen however when I navigate away from the screen and then return to it this does not work as expected. For example if the "manual" JRadioButton is selected which enables the JScrollPanes and the JComboBoxes when I navigate back to the page, clicking the "automatic" JRadioButton does not disable them, and vice versa. If I DOUBLE click on the JRadioButtons after returning to the screen the JScrollPanel and the JComboButtons appear to enable or disable correctly but when I click on them this is in fact not the case.
Has anyone seen this issue before or have any suggestions for how to fix it?
Thank you for your help
I have found the solution to this problem.
The issue was that I was creating my labelsAndCombos object in the wrong place. I was creating this object and it's components in the aboutToDisplayPanel() function and so it was being recreated each time I navigated back to the screen. This meant the enableDisable() function was still linked to the previous labelsAndCombos object, not the new one created and this was the cause of my issue.
I fixed this issue by creating the labelsAndCombos object in the initComponents() function.