![]() |
|||
![]() ![]() |
![]() |
|
![]() |
The integer returned from the dialog box can be used to determine which option the user chose. The returned value is the offset of the selected option in the option array parameter, or the CLOSED_OPTION if the user closed the dialog box without making a selection. Care must be taken when testing the return value from the showOptionDialog method. Due to the fact that the returned value is the 0-based position of the selected object in the array of options, changing the order of the options in the array forces the code that tests the returned value to be changed. This is suspect in the design of the showOptionDialog method. Any code using this method should contain comments reflecting these details. Rolling Your OwnThe various versions of the static showXXXDialog methods are adequate for a wide variety of dialog boxes. However, they dont solve every situation. For situations where there is not an appropriate canned dialog box, an instance of the JOptionPane class can be created and used as desired. The JOptionPane class contains a multitude of public constructors for this purpose. Most of these map to one or more of the static showXXXDialog methods. In these cases, the showXXXDialog should be used. If your application contains Actions to perform control operations, it may be desirable to have an action-aware JOptionPane. This dialog box would act similar to the action-aware JMenu and JToolBar classes presented in Chapter 9, Menus and Toolbars. This functionality can be accomplished by creating the JOptionPane with the proper controls. The following demonstrates how to make an action-aware JOptionPane dialog box. JButton[] options = new JButton[2]; options[0] = new JButton( Exit ); options[0].addActionListener( new ExitAction() ); options[1] = new JButton( Cancel ); options[1].addActionListener( new NoOpAction() ); JOptionPane pane = new JOptionPane( Exit Application, JOptionPane.QUESTION_MESSAGE, JOptionPane.DEFAULT_OPTION, null, options, options[1] ); for( int i = 0; i < options.length; i++ ) options[i].addActionListener( new OptionPaneActionListener( pane ) ); JDialog dialog = pane.createDialog( parent, Exit Confirmation ); dialog.setVisible( true ); class OptionPaneActionListener extends Object implements ActionListener { /** * The JOptionPane which the action source * we are listening on lives. **/ JOptionPane optionPane; public OptionPaneActionListener( JOptionPane optionPane ) { this.optionPane = optionPane; } // OptionPaneActionListener public void actionPerformed( ActionEvent event ) { optionPane.setValue( event.getSource() ); } // actionPerformed} // OptionPaneButton In this example, and array of buttons is created, one button for each action in the dialog box. The Action is added as an ActionListener for the button. This will cause the Action to be executed when the button is pressed. The array of buttons is passed as the options to the JOptionPane constructor. When a Component is used as an option to a JOptionPane, it is the responsibility of the Component to call the setValue method of the JOptionPane instance when activated. The value property is set to the Component itself. This allows the caller to determine which option the user chose. The value is a bound property, allowing listeners to receive notifications when the value property changes. To perform this function, another ActionListener is added to each button. This ActionListener is a class dedicated to calling the setValue method of the JOptionPane it is given at construction time. The call is made when the listener receives an actionPerformed message. The value is set to the source of the ActionEvent. After the ActionListeners are added, the createDialog method of the JOptionPane class is called. This method creates a JDialog instance with the given title. The JOptionPane is added as the center component to the content pane of the dialog box. The createDialog method also adds an internal WindowListener to the JDialog instance. The WindowListener catches WindowClosing events, and sets the value property of the JOptionPane to null when such an event is received. This allows clients to know when the dialog box was closed without the user choosing an option. The createDialog method also adds a property change listener to the JOptionPane. When the INPUT_VALUE_PROPERTY or VALUE_PROPERTY change, the window is closed and disposed of. This is how a custom Component calling the setValue method closes the dialog box. The custom Component does not have to close the dialog box itself. This example demonstrates the responsibilities of Components added to a JOptionPane. However, it is clumsy to use. Instead of manually creating the buttons and adding the required listeners, it would be more desirable to pass the JOptionPane an array of Action instances, and have JOptionPane do the work. This is not possible with the current implementation of the JOptionPane class. However, it is a simple matter to extend the JOptionPane class to provide the desired functionality. The ActionOptionPane is such a class (see Listing 18.1). It contains a constructor matching the full function JOptionPane constructor and overrides the setOptions method. In the constructor, the arguments are passed to the JOptionPane constructor, with the exception of the options and initialValue parameters. These are passed as null. This will cause an instance of the JOptionPane class to be created and configured with the proper message, message type, option type, and icon. As before, an ActionListener is created to call the setValue method when an option is selected. Next, the setOptions method is called with the original options array. Finally, the initial value is set to the given Object.
|
![]() |
|