I have an extensive Gui with many components in it. There is a updateEnable() methods that updates the enable state of all the components based on some configuration.
I basically first set their enable state to true and then disable some of them (based on the configuration):
private void updateEnable() {
enableAllRec(panel, true);
// disable some components
}
private void enableAllRec(Container root, boolean b) {
if (root == null) return;
for (Component c : root.getComponents()) {
c.setEnabled(b);
if (c instanceof Container) enableAllRec((Container) c, b);
}
}
The reason I do it like that is that some of the components are not stored as member variables and I don’t have access to them. They however can change their state because I initialized some of them like this (for example):
final JLabel exampleLabel = new JLabel("yoink");
final JCheckBox exampleCheckBox = new JCheckBox("boing");
exampleCheckBox.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
exampleLabel.setEnable(exampleCheckBox.isSelected());
}
});
Now my problem is the following: When I call updateEnable(), some of the (stored) Components may flicker because they are enabled and then disabled again after some time. I would like to prevent that. My idea is to somehow prevent the GUI from refreshing until the very end of updateEnable() and then perform an updateUI(). But this is not very elegant AND I have no idea how to prevent the GUI from updating.
Am I missing a very elegant alternative solution for this problem?
Thanks a lot,
Stefan
Ok, my approach seems to work. I just extended the
enableAllRec(Container root, boolean b)to include some exclusions:This way, I can determen all the components that I want to have enabled/disabled for sure, add them to their corresponding sets and thenn call the enableAllRec. Example:
Obviously, this is not ideal for large sets, but it is sufficient for my needs (arround 800 Components and the sets contain no more than arround 20-30 components). I suspect this method to easily handle larger sets as well.
In the above example, it makes no sense to introduce a disableList (as all components will be disabled by default anyway). So depending on the default value, one can leave out the corresponding set. This should preferably be done in
enableAllRec()of course.