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:
jbInit()
method in which JDeveloper puts
property setters and other initialization code used by the Java Visual
Editor. The jbInit()
method will be invoked when using
any of the constructors.
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:
Choose File
New to locate the New Dialog dialog in the New Gallery.
In the Categories list, expand Client Tier and select Swing/AWT.
In the Items list, select
Dialog and click OK to launch the New
Dialog dialog.
java.awt.Dialog
or
javax.swing.JDialog
.
The dialog box is displayed as a .java
source file in
the Navigation window.
To view the a dialog box in JDeveloper:
Right-click the file in the Navigator and choose Open and click the Design tab to use the interactive UI design tools, such as the Component Palette and the Property Inspector.
Right-click the file in the Navigator and choose Open to begin customizing the source code directly.
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:
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.)
actionPerformed
event by entering a line of code inside the event
handler that looks like this:
dialog1.show();
show()
method call, because show()
doesn't
return until the modal dialog box is dismissed. You can use a
result property to determine whether the OK
or Cancel button was pressed.
show()
returns
immediately. Because of this, the dialog class itself will need to
expose events for each of the button presses. When using the
dialog box, you will need to register listeners to the dialog's
events, and place code in the event handling methods to use
property getters to get the information out of the dialog box.
Copyright © 1997, 2004, Oracle. All rights reserved.