0

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

brimborium
  • 9,362
  • 9
  • 48
  • 76
  • 1
    you can find your answer [here](http://stackoverflow.com/questions/4065456/prevent-java-from-repainting-the-content-of-a-jpanel-while-updating). – Grims May 11 '12 at 11:04
  • Thanks, that is exactly what I was looking for. Although an idea for a more elegant way came to my mind. I will post an answer if it works well. – brimborium May 11 '12 at 12:13
  • Ok, I just posted my current solution and accepted it as it seems to work fine for me. mgarins caching idea is also a possibility although I don't like to create a Map containing every Component of my gui. My approach only creates a set of the special cases which I think is better. – brimborium May 15 '12 at 11:04

2 Answers2

1

In case your components might be enabled/disabled a few times in a row due to some logical changes and calculations - you should perform that calculation before applying some of the changes to visual components. In that case you will change components state straight into final one.

Mikle Garin
  • 10,083
  • 37
  • 59
  • The problem here is, that I don't have access to some of them except through `Container.getComponents()`. – brimborium May 11 '12 at 12:09
  • 1
    Than either make it in two runs or cache final states into some map like Map and after that run through that map setting final values into setEnabled method of the components. – Mikle Garin May 11 '12 at 14:37
  • The caching idea would be nice too although it would create a Map of all my components, which i think is not neccessary, because most of them are just fine with the `enableAllRec()` default set... – brimborium May 15 '12 at 11:01
0

Ok, my approach seems to work. I just extended the enableAllRec(Container root, boolean b)to include some exclusions:

private void enableAllRec(Container root, boolean defaultState, Set<Component> disableList, Set<Component> enableList) {
  if (root == null) return;
  for (Component c : root.getComponents()) {
    if (disableList != null && disableList.contains(c)) {
      c.setEnabled(false);
      disableList.remove(c);
    } else if (enableList != null && enableList.contains(c)) {
      c.setEnabled(true);
      enableList.remove(c);
    } else c.setEnabled(defaultState);
    if (c instanceof Container) enableAllRec((Container) c, defaultState, disableList, enableList);
  }
}

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:

Set<Component> enableList = new HashSet<Component>();
enableList.add(searchFreqButton);
enableList.add(advMeasButton);
enableAllRec(configPanel, false, null, enableList);

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.

brimborium
  • 9,362
  • 9
  • 48
  • 76