Swing: Data entry components

advertisement
Java Note
Swing - 7: Data Entry Components - Dialogs
1
2
Purpose ................................................................................1
Message dialogs .......................................................................1
2.1
Basic form ........................................................................1
2.2
Message type ....................................................................2
2.3
Text size and presentation ....................................................4
3 Confirmation dialogs .................................................................4
4 Input dialogs ..........................................................................6
1
Purpose
This document is part of a series in which we look at Swing components that
enable a user to enter or select data. The present document introduces various
types of user dialogs, which are implemented in the class JOptionPane.
2
Message dialogs
2.1
Basic form
The simplest dialog shows a dialog box with the title "Message" and with just an
"OK" button for confirmation. Closing the dialog box has the same effect as
pressing "OK", so there is no choice for the user.
To
produce
this
dialog
call
the
static
method
JOptionPane.showMessageDialog(). The basic method (there are several
overloaded versions) takes two arguments:


A type Component argument, typically a reference to the frame or window
A type Object argument, normally a string containing the message
Example (not a full program but only the relevant parts):
JFrame frame;
(...)
void showDialog(String text)
{
JOptionPane.showMessageDialog(frame, text);
}
public void actionPerformed(ActionEvent e)
{
Object eventSource = e.getSource();
String comm = e.getActionCommand();
if (comm.equals("=exit")) {
showDialog("You pressed the EXIT button");
frame.dispose();
System.exit(0);
}
}
Output:
2.2
Message type
Many methods of JOptionPane take an integer message type as argument.
Following message types are defined:
ERROR_MESSAGE
INFORMATION_MESSAGE
PLAIN_MESSAGE
QUESTION_MESSAGE
WARNING_MESSAGE
The versions of showMessageDialog() that accept a message type also take
a string argument to specify the title of the message box. Below are examples
of each type along with a screen shot of the result:
JOptionPane.showMessageDialog(frame,
"Error", JOptionPane.ERROR_MESSAGE);
"This
is
an
error",
JOptionPane.showMessageDialog(frame, "This is information",
"Information", JOptionPane.INFORMATION_MESSAGE);
JOptionPane.showMessageDialog(frame,
"Just
message", "Plain", JOptionPane.PLAIN_MESSAGE);
a
plain
JOptionPane.showMessageDialog(frame,
"May
I
ask
something?", "Question", JOptionPane.QUESTION_MESSAGE);
JOptionPane.showMessageDialog(frame,
JOptionPane.WARNING_MESSAGE);
"Beware!",
you
"Warning",
2.3
Text size and presentation
The text can be of any length; Swing will adapt the size of the dialog box so
that the text is fully shown. Newline characters ("\n") are honoured, so you may
user these to divide long texts into multiple lines for better readability.
Example:
String aboutText = "WinDialog - 1.00\n" +
"Patch level: 00\n" +
"Build date: 2009-09-09\n" +
"(c) Mark's Software Factory";
JOptionPane.showMessageDialog(frame, aboutText, "about",
JOptionPane.PLAIN_MESSAGE);
3
Confirmation dialogs
A confirmation dialog lets the user choose between Yes, No and Cancel.
The static method to use is JOptionPane.showConfirmDialog(); the
method returns an int indicating the user's reply:
YES_OPTION
NO_OPTION
CANCEL_OPTION
CLOSED_OPTION
choice
=
=
=
=
yes
no
cancel
dialog box closed without making a
The simplest form takes a reference to the frame or window and the text;
int reply
exit?");
=
JOptionPane.showConfirmDialog(frame,
"Really
Simple code to confirm an operation (here exiting the program):
boolean confirmExit()
{
int reply = JOptionPane.showConfirmDialog(frame, "Really
exit?");
return (reply==JOptionPane.YES_OPTION ? true : false);
}
public void actionPerformed(ActionEvent e)
{
Object eventSource = e.getSource();
String comm = e.getActionCommand();
if (comm.equals("=exit")) {
if (confirmExit()) {
frame.dispose();
System.exit(0);
}
}
}
By using another version of the method you can control which choices appear in
the dialog:
 Yes, No and Cancel (default): YES_NO_CANCEL_OPTION
 Yes and No: YES_NO_OPTION
 OK and Cancel: OK_CANCEL_OPTION
This method again wants the title:
int reply = JOptionPane.showConfirmDialog(frame,
exit?", "Confirm exit", JOptionPane.YES_NO_OPTION);
"Really
int reply = JOptionPane.showConfirmDialog(frame, "Really
exit?", "Confirm exit", JOptionPane.OK_CANCEL_OPTION);
As with message dialogs you can change the message type, which affects the
icon being displayed
Example:
boolean confirmExit()
{
String warnText = "Data will be lost.\n" +
"Do you really want to exit?";
int reply = JOptionPane.showConfirmDialog(frame, warnText,
"Confirm exit",
JOptionPane.OK_CANCEL_OPTION,
JOptionPane.WARNING_MESSAGE);
return (reply==0 ? true : false);
}
Result:
4
Input dialogs
An input dialog lets a user type data into an input field. The static method is
JOptionPane.showInputDialog()
Here is the simplest form:
void getUserInput()
{
String city = JOptionPane.showInputDialog(frame, "Enter the
city:");
}
Result:
The data typed is stored into the string return value. If the user presses Cancel
or closes the dialog, the string is explicitly set to null.
You can pass a string to be used as the initial value:
city
=
JOptionPane.showInputDialog(frame,
city:", "London");
"Enter
the
Like with the other dialog types, there is a version of the method that sets the
title and the message type:
city
=
JOptionPane.showInputDialog(frame,
"Enter
the
city:", "Choose city", JOptionPane.INFORMATION_MESSAGE);
If you want a title and message type and also an initial value, then you must
use a rather complex signature:
public static Object showInputDialog(Component parentComponent,
Object message,
String title,
int messageType,
Icon icon,
Object[] selectionValues,
Object
initialSelectionValue)
You must then provide values for the icon and selectionValues (to be
tested - received a compilation error)
Download