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

Lambertus osm at na1400.info
Sun May 2 13:37:36 BST 2010


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?





More information about the josm-dev mailing list