So far, all our user interface components have appeared inside a frame window that was created in the application. This is the most common situation if you writeapplets that run inside a web browser. But if you write applications, you usually want separate dialog boxes to pop up to give information to or get information from the user.Just as with most windowing systems, AWT distinguishes between modal and modeless dialog boxes. A modal dialog box won't let users interact with the remaining windows of the application until he or she deals with it. You use a modal dialog box when you need information from the user before you can proceed with execution. For example, when the user wants to read a file, a modal file dialog box is the one to pop up. The user must specify a file name before the program can begin the read operation. Only when the user closes the (modal) dialog box can the application proceed.A modeless dialog box lets the user enter information in both the dialog box and the remainder of the application. One example of a modeless dialog is a toolbar. The toolbar can stay in place as long as needed, and the user can interact with both the application window and the toolbar as needed.We start this section with the simplest dialogs—modal dialogs with just a single message. Swing has a convenient JOptionPane class that lets you put up a simple dialog without writing any special dialog box code. Next, you see how to write more complex dialogs by implementing your own dialog windows. Finally, you see how to transfer data from your application into a dialog and back.We conclude this section by looking at two standard dialogs: file dialogs and color dialogs. File dialogs are complex, and you definitely want to be familiar with the Swing JFileChooser for this purpose—it would be a real challenge to write your own. The JColorChooser dialog is useful when you want users to pick colors.
Option Dialogs
Swing has a set of ready-made simple dialogs that suffice when you need to ask the user for a single piece of information. The JOptionPane has four static methods to show these simple dialogs:
showMessageDialog | Show a message and wait for the user to click OK |
showConfirmDialog | Show a message and get a confirmation (like OK/Cancel) |
showOptionDialog | Show a message and get a user option from a set of options |
showInputDialog | Show a message and get one line of user input |
Figure 9-46 shows a typical dialog. As you can see, the dialog has the following components:
- An icon
- A message
- One or more option buttons
Figure 9-46. An option dialog
The input dialog has an additional component for user input. This can be a text field into which the user can type an arbitrary string, or a combo box from which the user can select one item.
The exact layout of these dialogs, and the choice of icons for standard message types, depend on the pluggable look and feel.
The icon on the left side depends on one of five message types:
ERROR_MESSAGE INFORMATION_MESSAGE WARNING_MESSAGE QUESTION_MESSAGE PLAIN_MESSAGE
The PLAIN_MESSAGE type has no icon. Each dialog type also has a method that lets you supply your own icon instead.
For each dialog type, you can specify a message. This message can be a string, an icon, a user interface component, or any other object. Here is how the message object is displayed:
String: | Draw the string |
Icon: | Show the icon |
Component: | Show the component |
Object[]: | Show all objects in the array, stacked on top of each other |
any other object: | Apply toString and show the resulting string |
You can see these options by running the program in Example 9-18.
Of course, supplying a message string is by far the most common case. Supplying a Component gives you ultimate flexibility because you can make the paintComponentmethod draw anything you want.
The buttons on the bottom depend on the dialog type and the option type. When calling showMessageDialog and showInputDialog, you get only a standard set of buttons (OK and OK/Cancel, respectively). When calling showConfirmDialog, you can choose among four option types:
DEFAULT_OPTION YES_NO_OPTION YES_NO_CANCEL_OPTION OK_CANCEL_OPTION
With the showOptionDialog you can specify an arbitrary set of options. You supply an array of objects for the options. Each array element is rendered as follows:
String: | Make a button with the string as label |
Icon: | Make a button with the icon as label |
Component: | Show the component |
any other object: | Apply toString and make a button with the resulting string as label |
The return values of these functions are as follows:
showMessageDialog | None |
showConfirmDialog | An integer representing the chosen option |
showOptionDialog | An integer representing the chosen option |
showInputDialog | The string that the user supplied or selected |
The showConfirmDialog and showOptionDialog return integers to indicate which button the user chose. For the option dialog, this is simply the index of the chosen option or the value CLOSED_OPTION if the user closed the dialog instead of choosing an option. For the confirmation dialog, the return value can be one of the following:
OK_OPTION CANCEL_OPTION YES_OPTION NO_OPTION CLOSED_OPTION
This all sounds like a bewildering set of choices, but in practice it is simple:
1. | Choose the dialog type (message, confirmation, option, or input). |
2. | Choose the icon (error, information, warning, question, none, or custom). |
3. | Choose the message (string, icon, custom component, or a stack of them). |
4. | For a confirmation dialog, choose the option type (default, Yes/No, Yes/No/Cancel, or OK/Cancel). |
5. | For an option dialog, choose the options (strings, icons, or custom components) and the default option. |
6. | For an input dialog, choose between a text field and a combo box. |
7. | Locate the appropriate method to call in the JOptionPane API. |
For example, suppose you want to show the dialog in Figure 9-46. The dialog shows a message and asks the user to confirm or cancel. Thus, it is a confirmation dialog. The icon is a warning icon. The message is a string. The option type is OK_CANCEL_OPTION. Here is the call you would make:
int selection = JOptionPane.showConfirmDialog(parent, "Message", "Title", JOptionPane.OK_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE); if (selection == JOptionPane.OK_OPTION) . . .
0 comments:
Post a Comment