[josm-dev] ExtendedDialog: enable/disable OK and Cancel button

Lambertus osm at na1400.info
Sun May 2 20:12:12 BST 2010


On 05/02/2010 04:02 PM, Sebastian Klein wrote:
> The button is inside another panel, so you might want to try recursion.
> As a start, here is an example from TaggingPreset.java, l. 736:
>
>       static void setEnabledRec(Container root, boolean enabled) {
>           root.setEnabled(enabled);
>           Component children[] = root.getComponents();
>           for(int i = 0; i<  children.length; i++) {
>               if(children[i] instanceof Container) {
>                   setEnabledRec((Container)children[i], enabled);
>               } else {
>                   children[i].setEnabled(enabled);
>               }
>           }
>       }
>
> (This disables/enables all components in a given panel.)
>
>
Thanks that was a great help. There are indeed more nested containers. I 
needed to reverse the if-then-else as JButton in itself is also a 
subclass of Container. The code now looks like this:

     private static JButton getButton(Container root, String caption) {
         Component children[] = root.getComponents();
          for (Component child : children) {
              JButton b;
              if (child instanceof JButton) {
                 b = (JButton) child;
                 if (caption.equals(b.getText())) return b;
             } else if (child instanceof Container) {
                   b = getButton((Container)child, caption);
                   if (b != null) return b;
              }
          }
         return null;
     }

Thanks again.



More information about the josm-dev mailing list