Learning Java Chapter 14: Using Swing Components
Let's look at examples of each kind of dialog. The following code produces
a message dialog:
JOptionPane.showMessageDialog(f, "You have mail.");
The first parameter to showMessageDialog( ) is the parent
component (in this case f, an existing JFrame).
The dialog will be centered on the parent component. If you pass
null for the parent component, the dialog is centered in your
screen. The dialogs that JOptionPane displays are
modal, which means they block other input to your application while
they are showing.
Here's a slightly fancier message dialog. We've specified a title for the
dialog and a message type, which affects the icon that is displayed:
JOptionPane.showMessageDialog(f, "You are low on memory.",
"Apocalyptic message", JOptionPane.WARNING_MESSAGE);
Here's how to display the confirmation dialog shown in Figure 14-10:
int result = JOptionPane.showConfirmDialog(null,
"Do you want to remove Windows now?");
In this case, we've passed null for the parent component.
Special values are returned from showConfirmDialog( ) to
indicate which button was pressed. There's a full example below that shows
how to use this return value.
Sometimes you need to ask the user to type some input. The following code
puts up a dialog requesting the user's name:
String name = JOptionPane.showInputDialog(null,
"Please enter your name.");
Whatever the user types is returned as a String, or
null if the user presses the Cancel button.
The most general type of dialog is the option dialog. You supply an array
of objects that you wish to be displayed; JOptionPane takes
care of formatting them and displaying the dialog. The following example
displays a text label, a JTextField, and a
JPasswordField. (Text components are described in the next
chapter.)
JTextField userField = new JTextField( );
JPasswordField passField = new JPasswordField( );
String message = "Please enter your user name and password.";
result = JOptionPane.showOptionDialog(f,
new Object[] { message, userField, passField },
"Login", JOptionPane.OK_CANCEL_OPTION,
JOptionPane.QUESTION_MESSAGE,
null, null, null);
We've also specified a dialog title ("Login") in the call to
showOptionDialog( ). We want OK and Cancel
buttons, so we pass OK_CANCEL_OPTION as the dialog type. The
QUESTION_MESSAGE argument indicates we'd like to see the
question mark icon. The last three items are optional: an
Icon, an array of different choices, and a current selection.
Since the icon parameter is null, a default is used. If the
array of choices and the current selection parameters were not
null, JOptionPane might try to display the
choices in a list or combo box.
New on the Java Boutique:
New Review:
Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling
API boasts simplicity, ease-of-integration, a well-rounded feature
set, and it's free!
New Applet:
Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA
sequences into three useful formats.
Elsewhere on internet.com:
WebDeveloper Java
Lots of Java information on webdeveloper.com
WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.
ScriptSearch Java
Hundreds of free Java code files to download.
jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.
|