cornercorner
FeaturesPluginsDocs & SupportCommunityPartners

DialogDescriptor

This class has five constructors:
  1. DialogDescriptor(Object innerPane, String title)  -- create modal dialog descriptor with:
    • inner part
    • given title
    Ok/cancel buttons are displayed with default alignment, no help available. All buttons will close the dialog and the getValue() will provide the pressed option.
     
  2. DialogDescriptor(Object innerPane, String title, boolean isModal, ActionListener bl) -- create dialog descriptor with: (for more information see fourth constructor)
    • inner part
    • given title
    • modal status
    • listener for the user's button presses or null for default close action on all options
    Ok/cancel buttons are displayed with default alignment, no help available. If bl is not null, then it will receive notifications when the user presses the buttons. (If no listener is specified, it's still possible to retrieve the user-selected button using getValue().)
     
  3. DialogDescriptor(Object innerPane, String title, boolean isModal, int optionType, Object initialValue, ActionListener bl) -- create dialog descriptor with: (for more information see fourth constructor)
    • inner part
    • given title
    • modal status
    • option type
      • display defaults -- NotifyDescriptor.DEFAULT_OPTION
      • display yes/no buttons -- NotifyDescriptor.YES_NO_OPTION
      • display yes/no/cancel buttons -- NotifyDescriptor.YES_NO_CANCEL_OPTION
      • display ok/cancel buttons -- NotifyDescriptor.OK_CANCEL_OPTION
    • default option
    • listener for the user's button presses or null for default close action on all options
    Options have default alignment, no help available. If the action listener is null, all option buttons will close the dialog and the getValue() will provide the pressed option.
     
  4. DialogDescriptor(Object innerPane, String title, boolean isModal, int optionType, Object initialValue, int optionsAlign, HelpCtx helpCtx, ActionListener bl)  -- create dialog descriptor with:
    • inner part
    • given title
    • modal status
    • option type
      • display defaults -- NotifyDescriptor.DEFAULT_OPTION
      • display yes/no buttons -- NotifyDescriptor.YES_NO_OPTION
      • display yes/no/cancel buttons -- NotifyDescriptor.YES_NO_CANCEL_OPTION
      • display ok/cancel buttons -- NotifyDescriptor.OK_CANCEL_OPTION
    • default option
    • possibility of specifying custom array of options and their alignment
      • Alignment to put options in the bottom part (buttons are in the right-down corner) -- DialogDescriptor.BOTTOM_ALIGN
      • Alignment to place options vertically in the right part (buttons are in the right-up corner) -- DialogDescriptor.RIGHT_ALIGN
      • Alignment to place options in the default manner (bottom align) -- DialogDescriptor.DEFAULT_ALIGN
    • helpCtx - help context specifying help page
    • bl - listener for the user's button presses or null for default close action on all options

     
  5. DialogDescriptor(Object innerPane, String title, boolean modal, Object[] options, Object initialValue, int optionsAlign, HelpCtx helpCtx, ActionListener bl) -- create dialog descriptor with:
    • inner part
    • given title
    • modal status
    • options - array of custom options (null means no options at all)
      • predefined buttons:
        • yes -- NotifyDescriptor.YES_OPTION
        • no -- NotifyDescriptor.NO_OPTION
        • cancel -- NotifyDescriptor.CANCEL_OPTION
        • ok -- NotifyDescriptor.OK_OPTION
        • closed -- NotifyDescriptor.CLOSED_OPTION
    • initialValue - default option
    • optionsAlign - specifies where to place options in the dialog
    • helpCtx - help context specifying help page
    • If the action listener is null, all option buttons will close the dialog and the getValue() will provide the pressed option.
    If the action listener is null, all option buttons will close the dialog and the getValue() will provide the pressed option. When a custom options set is provided, if any of the standard options (OK_OPTION, CLOSE_OPTION or CANCEL_OPTION) are used, the dialog will close when the button for this option is pressed, for custom options, closing the dialog is left for the ActionListener.
How to use first constructor:
 
  1. Create new class, which extends JPanel. All components you want to have in the dialog (ok and cancel buttons are created default) should be on this panel. A good way is to use Form Editor (New -> Swing Forms -> JPanel).
  2. It's recommended you create method requestFocus () . It sets the focus for the component which should have default focus.


General example:

class MyPanel extends javax.swing.JPanel {
       //buttons, fields, labels, etc.

        ...
 

       public void requestFocus () { //set focus for one components (myField in this case)
            myField.requestFocus ();
       }

        ...

}

How to use this dialog in your code:

MyPanel mp = new MyPanel(); // create new MyPanel
DialogDescriptor dd = new DialogDescriptor (mp, "Text in title"); //create new DialogDescriptor
mp.requestFocus(); // set focus to component which was specified in MyPanel's requestFocus() method
TopManager.getDefault ().createDialog (dd).show (); //show dialog

//what to do when dialog was closed
if (dd.getValue () == DialogDescriptor.OK_OPTION) {
    //ok button was pressed

}
else{
    //cancel button was pressed

}
 
 
 

How to use fourth constructor:
 

  1. Create new class which extends JPanel. All components what you want to have in the dialog (ok and cancel buttons are created default) should be on this panel. A good way is to use the Form Editor (New -> Swing Forms -> JPanel).
  2. It's recommended you create method requestFocus () . It sets the focus for the component which should have default focus.
  3. If you would like have control over the buttons which are created by DialogDescriptor (OK, Cancel), you can use the parameter ActionListener bl. This parameter specifies the listener for these buttons (usually the same class, see example).


General example:

class MyPanel extends javax.swing.JPanel implements java.awt.event.ActionListener {
       //buttons, fields, labels, etc.

        ...
 

       public void requestFocus () { //set focus for one components (myField in this case)
            myField.requestFocus ();
       }

        ...

       public void actionPerformed(final java.awt.event.ActionEvent ap) { // handling code for buttons

        ...

       }

}

How to use this dialog in your code:

MyPanel mp = new MyPanel(); // create new MyPanel
DialogDescriptor dd = new DialogDescriptor(mp,
            "Text in title",
            true,
            NotifyDescriptor.OK_CANCEL_OPTION,
            NotifyDescriptor.OK_OPTION,
            DialogDescriptor.BOTTOM_ALIGN,
            null,
            mp);
mp.requestFocus(); // set focus to component which was specified in MyPanel's requestFocus() method
TopManager.getDefault ().createDialog (dd).show (); //show dialog
 
 
 

How to use fifth constructor:
 

  1. Create a new class which extends JPanel. All components that you want to have in the dialog should be on this panel. All buttons must be defined in parameter options. A good way is to use Form Editor (New -> Swing Forms -> JPanel).
  2. Specify default button in parameter initialValue.
  3. It's recommended you create method requestFocus () . It sets the focus for the component which should have default focus.
  4. If you would like have control over the buttons which are created by DialogDescriptor (OK, Cancel), you can use the parameter ActionListener bl. This parameter specifies the listener for these buttons (usually the same class, see example).
General example:
class MyPanel extends javax.swing.JPanel implements java.awt.event.ActionListener {
       //buttons, fields, labels, etc.

        ...
 

       public void requestFocus () { //set focus for one components
            myField.requestFocus ();
       }

        ...

       public void actionPerformed(final java.awt.event.ActionEvent ap) { // handling code for buttons

        ...

       }

}

MyPanel mp = new MyPanel(); // create new MyPanel
Object [] options =  {  new JButton ("Choice 1"),
                        new JButton ("Choice 2")};

DialogDescriptor dd = new DialogDescriptor (mp,
                            "Text in title",
                             true,
                             options,
                             null,
                             DialogDescriptor.DEFAULT_ALIGN,
                             null,
                             mp); //create new modal DialogDescriptor with defined ActionListener
mp.requestFocus(); // set focus to component which was specified in MyPanel's requestFocus() method
TopManager.getDefault ().createDialog (dd).show (); //show dialog
 

Tip:

TopManager.getDefault ().createDialog (dd) returns a standard AWT Dialog, so, for example, you can easily change the size:

Dialog myDialog=TopManager.getDefault ().createDialog (dd);
myDialog.setSize(640,480);
myDialog.show ();

The instance returned by TopManager.getDefault ().createDialog (dd); is valid until dialog is visible. You cannot close the dialog and use show() method to reopen because of the AWT father in the hierarchy. If you would like create the same dialog again, you can have a stored instance of the DialogDescriptor and create the dialog with TopManager.getDefault ().createDialog (dd);
 
 

Companion
Projects:
MySQL Database Server   GlassFish Community: an Open Source Application Server   Open Solaris  Open JDK: an Open SourceJDK   Mobile & Embedded Community     Sponsored by 
Sponsored by Sun Microsystems