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

Sebastian Klein bastikln at googlemail.com
Sun May 2 15:02:09 BST 2010


Lambertus wrote:
> While creating an instace of org.openstreetmap.josm.gui.ExtendedDialog 
> you can add default buttons like OK and Cancel. Once the buttons are 
> created I would like to disable/enable these buttons based upon some 
> input field checks but I have no idea how to locate the appropriate 
> button object.
> 
> I've tried looping though the dialog components, check for JButton 
> components and then look for JButton with e.g. "OK" text like so:
> 
>      private JButton getButton(String caption) {
>          Component components[] = dialog.getComponents();
>          for (Component c : components) {
>              System.out.println("getButton: "+c.getName());
>              if (c instanceof JButton) {
>                  JButton b = (JButton) c;
>                  if (caption.equals(b.getText())) {
>                      return b;
>                  }
>              }
>          }
>          return null;
>      }
> 
> This function is called from the following code:
> 
>      JButton okButton = getButton("OK");
>          if (okButton != null)
>              okButton.setEnabled(false);
> 
> But of all the components the dialog is made up, there is only one line 
> of debugoutput: "getButton: null". I have no clue what to try next. Any 
> ideas?

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.)


Sebastian




More information about the josm-dev mailing list