![]() |
|||
![]() ![]() |
![]() |
![]()
|
![]() |
Message and InformationalThe JOptionPane class can be used to present an informational dialog box to the user. One of the overloaded variants of the static showMessageDialog method can be used to do this. The complete signature of this method is the following: showMessageDialog(Component, Object, String, int, Icon) The Component and Object parameters are required. The remaining parameters are optional. Default values for the optional parameters display an informational dialog box with a title of Message and an OK button. The following application can be used to display the simple dialog box shown in Figure 18.2. The following code provides a vivid example of the power of the JOptionPane class. In a single line, a complete modal dialog box is displayed. This simple application framework can be used for the JOptionPane examples throughout this chapter. The relevant JOptionPane method can be substituted for the one in the main method of the JOptionTest application shown next. package com.foley.test; import javax.swing.*; /** * An application that displays a JOptionPane dialog box. * * @author Mike Foley **/ public class JOptionTest extends Object { /** * Application entry point. * Display a dialog box from the JoptionPane class. * * @param args Command line parameter. Not used. **/ public static void main( String args[] ) { JOptionPane.showMessageDialog( null, Invalid Entry ); System.exit(0); } // main } // JOptionTest
It is a simple matter to change the style of the dialog box. By specifying the type parameter, the default icon and title for the dialog box can be changed. This provides an easy method to specify the urgency of the dialog box. For example, adding the ERROR_MESSAGE message constant, the urgency of the dialog box is considered to be greater than the informational dialog box shown in Figure 18.2. The dialog box produced with the following line of code is shown in Figure 18.3. Passing the showMessageDialog a null parent causes the dialog box to be centered on the screen. Passing a non-null parent parameter will cause the dialog box to be centered relative to the frame containing the component passed to the showMessageDialog method. Notice how the given message parameter appears in the center of the dialog box, and the title is used for the title of the dialog box. JOptionPane.showMessageDialog( null, Invalid Entry, Error, JOptionPane.ERROR_MESSAGE );
By changing the type of the dialog, the JOptionPane used a different icon. This gives the user a visual cue for the type of the information being presented. If the default icons dont meet your requirements, the icon to be placed in the dialog box can be specified. The following modification to the showMessageDialog method call creates a dialog box containing the stop icon (see Figure 18.4): JOptionPane.showMessageDialog( null, Invalid Entry, Error, JOptionPane.ERROR_MESSAGE, stop );
This example assumes that the icon named stop has been previously loaded. Loading images and creating icons was discussed in Chapter 5, Basic Components, and will not be repeated here. Yes/No/CancelIn many situations, you need a dialog box to ask the user for confirmation before performing an action. The JOptionPane class provides the showConfirmDialog method to present the user with a modal dialog box asking for confirmation. A simple invocation of this method is shown next. The dialog box created by this method call is shown in Figure 18.5. In the figure notice that the Yes button is selected by default. Once again, the power of the JOptionPane is shown in this example. One simple line of code produces a modal dialog box requesting user input. JOptionPane.showConfirmDialog( null, Continue? );
After presenting the dialog box, the thread in which the calling code is executing waits for the user input. This allows the JOptionPane method to return the selection the user makes. The returned value is an integer that can be one of the option constants defined in the JOptionPane class. Client code can check the returned value against these constants and take the appropriate action. This is demonstrated in the version of the JOptionTest application shown in the following code: package com.foley.test; import javax.swing.*; /** * An application that displays a JOptionPane dialog box. * * @author Mike Foley **/ public class JOptionTest extends Object { /** * Application entry point. * Display a dialog box from the JOptionPane class. * * @param args Command line parameter. Not used. **/ public static void main( String args[] ) { int result = JOptionPane.showConfirmDialog( null, Continue?, Confirmation Required, JOptionPane.YES_NO_OPTION ); switch( result ) { case JOptionPane.YES_OPTION: // Yes button was pressed. System.out.println( Yes button ); break; case JOptionPane.NO_OPTION: // No button was pressed. System.out.println( No button ); break; case JOptionPane.CANCEL_OPTION: // Cancel button was pressed. // (Not possible with the YES_NO_OPTION parameter. System.out.println( Cancel button ); break; case JOptionPane.CLOSED_OPTION: // Window was closed without a button being pressed. System.out.println( Closed button ); break; } // switch( result ) System.exit(0); } // main } // JOptionTest
|
![]() |
|