Part IV Dialog Boxes
- In This Part
- JOptionPane
- Choice Dialog Boxes
- JDialog
- Progress Monitor
Chapter 18 JOptionPane
- In This Chapter
- Common JOptionPane Configuration
- Predefined Dialog Boxes
- Rolling Your Own
- Internal Frame Versions
The JOptionPane class is the workhorse class for predefined dialog boxes contained in the JFC. In this chapter, you will learn how to use the JOptionPane class to
- Display message dialog boxes.
- Display and process confirmation dialog boxes.
- Display and process input dialog boxes.
- Display and process selection dialog boxes.
- Display and run Actions.
- Use internal frame versions of the dialog boxes.
The JOptionPane class provides a robust dialog box framework. It contains static methods for displaying simple dialog boxes to display information or prompt the user for input. These methods follow the naming convention showXXXDialog, where XXX is the type of dialog box to display. All JOptionPane dialog boxes are modal, meaning that they will block input to the rest of the application until the user finishes with the dialog box.
A JOptionPane dialog box consists of four regions: the icon, the message area, the input area, and the option button area. Each of these regions might or might not be shown. The exact size and placement of each region is dependent on the look-and-feel being used. Typical placement for these regions is shown on a sample dialog box shown in Figure 18.1.
Figure 18.1 JOptionPane regions.
The remainder of this chapter describes the myriad of options available in the JOptionPane, and gives examples of their use. This is followed by examples showing the use of each dialog box that can be constructed with the JOptionPane class.
Common JOptionPane Configuration
Convenience methods are provided in the JOptionPane class to display the predefined dialog boxes in the JOptionPane class. These methods conform to the following naming convention:
ShowXXXDialog
where XXX is the type of dialog box to display. Possible values for the type are Confirm, Input, Message, and Option. The parameters to the show method are dependent on the type of dialog box. However, many parameters are common to one or more of the dialog boxes. The remainder of this section will present the common parameters used by dialog boxes created in the JOptionPane class.
Parent Component
The first parameter to each of the show methods of the JOptionPane class is an AWT Component descendant used to determine the parent of the dialog box. If the component is not null, the frame of the component is used as the parent frame for the dialog box. The parents frame is also used for the placement of the dialog box. The exact placement is dependent on the look-and-feel being used. If the parent component is null, a default parent frame will be used. The default frame can be set with the static setRootFrame method. The frame given to this method will be used as the parent for dialog boxes opened with a null parent parameter. Finally, if the root frame has not been set and a null parent is provided, the SwingUtilities getSharedOwnerFrame method is used to get the shared owner frame for the application. See the discussion of the SwingUtilities class in Chapter 32, Swing Utilities, for a complete description of the shared owner frame.
The JOptionPane contains two static methods that are useful in a wide variety of situations, not just for dialog boxes. The first, named getFrameForComponent, will search the components lineage to find the Frame for a given component. It will return null if the component doesnt have a Frame in its ancestry. The second method, named getDesktopPaneForComponent, performs a similar function except it looks for a JDesktopPane in the components lineage. Both these methods are static. As such, they can be invoked from any class where the Frame or JDesktopPane of a component is required.
Message
Each of the predefined dialog boxes in the JOptionPane class takes an Object as a message parameter. The JOptionPane interprets the type of this Object and displays it accordingly. The most common usage for this parameter is a String. When a String is given for the message, it is displayed as is in the dialog box. However, the message parameter of the dialog box is far more powerful than a simple String viewer. If an Icon instance is given for the message, it is wrapped in an instance of the JLabel class and placed in the dialog box. If a Component instance is passed as the message, the Component is used directly. If another type of Object is passed as the message, its toString method will be called, and the returned String will be placed in the dialog box. Finally, an array of Object instances can be passed as the message. In this case, the logic previously described will be applied to each element in the array, and they will be stacked in the message area with lower array indexes on top of higher indexes. This provides a simple mechanism for a multi-line dialog box. An array of String instances can be passed as the message parameter. In this case, the String instances will be displayed in the dialog box one on top of the other.
|