Java Reference
In-Depth Information
public JDialog createDialog(Component parentComponent, String title)
public JInternalFrame createInternalFrame(Component parentComponent, String title)
Note
When using the
createDialog()
and
createInternalFrame()
methods to create a pop-up
window, selection of an automatically created button results in the closing of the created pop-up. You would
then need to ask the
JOptionPane
which option the user selected with
getValue()
and, if appropriate, get
the input value with
getInputValue()
.
■
The first argument to the methods is a component over which the pop-up window will be
centered. The second argument is the title for the pop-up window. Once you create the pop-up
window, whether it's a
JDialog
or
JInternalFrame
, you show it. The pop-up is then closed after
one of the components in the button area is selected, at which point, your program continues.
The following lines of source code show the creation of one such
JOptionPane
shown within a
JDialog
. The resulting pop-up window is shown in Figure 9-2.
JOptionPane optionPane = new JOptionPane("Continue printing?",
JOptionPane.QUESTION_MESSAGE, JOptionPane.YES_NO_OPTION);
JDialog dialog = optionPane.createDialog(source, "Manual Creation");
dialog.setVisible(true);
Figure 9-2.
Sample JOptionPane in a JDialog
After you create the
JOptionPane
, place it in a pop-up window, and show it, and the user
has responded, you need to find out what the user selected. The selection is provided via the
public Object getValue()
method of
JOptionPane
. The value returned by
getValue()
is
determined by whether an
options
array was provided to the
JOptionPane
constructor. If you
provide the array, the argument selected will be returned. If you don't provide the array, an
Integer
object is returned, and its value represents the position of the button selected within
the button area. In another case,
getValue()
could return
null
if nothing was selected, such as
when the
JDialog
is closed by selecting the appropriate window decoration from the title bar
of the pop-up window.