Creating a Dialog Box

A dialog box is a popup window with a border and a title. Dialog boxes are typically used to collect user input.

The New Dialog dialog creates a new class that extends Dialog or JDialog and adds it to the current project. It adds the necessary import statements, and it also does the following:

After adding the dialog box, you can design the dialog directly using the Java Visual Editor. This is how you add buttons and other controls to your new dialog box.

To create a dialog box:

  1. Open or create a project.
  2. Choose File then choose New to locate the New Dialog dialog in the New Gallery.

  3. In the Categories list, expand Client Tier and select Swing/AWT.

  4. In the Items list, select Dialog and click OK to launch the New Dialog dialog.

  5. In the New Dialog dialog, enter the name of the dialog box's package and class. The file name is automatically filled in for you; it is assigned the same name as the class and is saved to the package directory.
  6. Choose whether to extend java.awt.Dialog or javax.swing.JDialog.
  7. Click OK to create the dialog box and its source code.

    The dialog box is displayed as a .java source file in the Navigation window.

To view the a dialog box in JDeveloper:

Using a Dialog Box that is Not a Bean

Once the dialog box has been created and its UI designed, you will want to test or use your dialog box from some UI in your program. Here is a way to do this:

  1. Instantiate your dialog class from someplace in your code where you have access to a Frame which can serve as the parent Frame parameter in the dialog constructor. A typical example of this would be a Frame whose UI you are designing, which contains a Button or a MenuItem which is intended to bring up the dialog box. In applets, you can get the Frame by calling getParent() on the applet.

    For a modeless dialog box (which we are calling dialog1 in this example), you can use the form of the constructor that takes a single parameter (the parent Frame) as follows:

    Dialog1 dialog1=new Dialog1(this);

    For a modal dialog box, you will need to use a form of the constructor that has the boolean modal parameter set to true, such as in the following example:

    Dialog1 dialog1=new Dialog1(this, true);

    You can either place this line as an instance variable at the top of the class (in which case the dialog box will be instantiated during the construction of your Frame and be reusable), or you can place this line of code in the actionPerformed event handler for the button that invokes the dialog box (in which case a new instance of the dialog box will be instantiated each time the button is pressed.) Either way, this line instantiates the dialog box, but does not make it visible yet.

    (In the case where the dialog is a bean, you must set its frame property to the parent frame before calling show(), rather than supplying the frame to the constructor.)

  2. Before making the instantiated dialog box visible, you should set up any default values that the dialog box fields should display. If you are planning to make your dialog into a Bean, you need to make these dialog box fields accessible as properties. You do this by defining getter and setter methods in your dialog class.
  3. Next, you have to cause the dialog box to become visible during the actionPerformed event by entering a line of code inside the event handler that looks like this:

    dialog1.show();

  4. When the user presses the OK button (or the Apply button on a modeless dialog box), the code that is using the dialog box will need to call the dialog's property getters to read the user-entered information out of the dialog, then do something with that information.

About Containers