JOptionPane
JOptionPane makes it easy to pop up a standard dialog box
(free-standing window) that prompts users for a value or informs them
of something. While the class may appear complex because of the large
number of methods, almost all uses of this class are one-line calls to
one of the static showXxxDialog() methods shown below:
showMessageDialog |
tell the user about something that has happened |
showConfirmDialog |
asks a confirming question, like yes/no/cancel |
showOptionDialog |
a programmer-configurable ConfirmDialog |
showInputDialog |
prompt for some input
|
Each of these methods also comes in a
showInternalXxxDialog() flavor, which uses an internal
frame to hold the dialog box (see JInternalFrame).
Multiple convenience methods have also been defined (overloaded
versions of the basic methods that use different parameter lists).
JInternalFrame. A lightweight object that provides
many of the features of a native frame, including dragging, closing,
becoming an icon, resizing, title display, and support for a menu bar.
Generally, you create an instance and add it to a
JDesktopPane.
All dialogs are modal. Each showXxxDialog() method blocks
the current thread until the user's interaction is complete.
The basic appearance of one of these dialog boxes is generally similar
to the picture at the right, although the various look-and-feels are
ultimately responsible for the final result.
To create and use a JOptionPane object directly, the
standard pattern is roughly - |
icon |
message |
input value |
option buttons
|
|
JOptionPane pane = new JOptionPane( arguments );
pane.setXxx( ... );
JDialog dialog = pane.createDialog( parentComponent, title );
dialog.show();
Object selectedValue = pane.getValue();
if (selectedValue == null)
return CLOSED_OPTION;
Object[] options = pane.getOptions();
// If there is not an array of option buttons:
if (options == null) {
if (selectedValue instanceof Integer)
return ((Integer)selectedValue).intValue();
return CLOSED_OPTION;
}
// If there is an array of option buttons:
for (int count = 0, max = options.length; count < max; count++)
if (options[count].equals(selectedValue))
return count;
return CLOSED_OPTION;
JFileChooser provides a simple mechanism for the
user to choose a file. The example demonstrates the most
straight-forward usage of this class. More advanced features include:
standard filters, custom filters, customization of the UI, and handling
events. The following code demonstrates standard filters:
JFileChooser chooser = new JFileChooser();
ExtensionFileFilter filter = new ExtensionFileFilter();
filter.addExtension( "jpg" );
filter.addExtension( "gif" );
filter.setDescription( "JPG & GIF Images" );
chooser.setFileFilter( filter );
int returnVal = chooser.showOpenDialog( parent );
if (returnVal == JFileChooser.APPROVE_OPTION)
System.out.println( "You chose to open the file: "
+ chooser.getSelectedFile().getName() );
JColorChooser provides a pane of controls designed
to allow a user to manipulate and select a color. This class provides
3 levels of API:
- A static convenience method which shows a modal color-chooser
dialog and returns the color selected by the user.
- A static convenience method for creating a color-chooser dialog
where ActionListeners can be specified to
be invoked when the user presses one of the dialog buttons.
- The ability to create instances of JColorChooser panes directly
(within any container). PropertyChange
listeners can be added to detect when the current "color" property changes.
[Reference: Topley, pp477-523, 532, 548-556]