Java Reference
In-Depth Information
public void actionPerformed(ActionEvent e) {
if(e.getSource() == aboutItem) {
// Create about dialog with the menu item as parent
JOptionPane.showMessageDialog(this, // Parent
"Sketcher Copyright Ivor Horton 2001", // Message
"About Sketcher", // Title
JOptionPane.INFORMATION
_
MESSAGE); // Message type
}
}
Compile
SketchFrame
again and run Sketcher. When you
click on the
Help/About
menu item, you should get something
like the following:
The pretty little icon comes for free.
How It Works
All the work is done by the static
showMessageDialog()
method in the
JOptionPane
class. What
you get is controlled by the arguments that you supply, and the Swing look-and-feel in use. By default
this will correspond to what is usual for your system. On a PC running Windows, we get the icon you
see because we specified the message type as
INFORMATION
_
MESSAGE
. You can try plugging in the
other message types to see what you get.
Input Dialogs
JOptionPane
also has four static methods that you can use to create standard modal input dialogs:
showInputDialog(Object message)
This method displays a default modal input dialog with a text field for input. The message you pass as
the argument is set as the caption for the input field and the default also supplies an
OK
button, a
Cancel
button and
Input
as a title. For example, if you pass the message "
Enter Input
:" as the
argument in the following statement,
String input = JOptionPane.showInputDialog("Enter Input:");