Java Reference
In-Depth Information
the setting of the wantsInput property. The wantsInput property is set to true only when you
want the JOptionPane to provide its own input component. Because you're providing one, this
isn't necessary. The resulting pop-up window is shown in Figure 9-10. (The JSlider component
will be more fully described in Chapter 12.)
JOptionPane optionPane = new JOptionPane();
JSlider slider = OptionPaneUtils.getSlider(optionPane);
optionPane.setMessage(new Object[] { "Select a value: " , slider} );
optionPane.setMessageType(JOptionPane.QUESTION_MESSAGE);
optionPane.setOptionType(JOptionPane.OK_CANCEL_OPTION);
JDialog dialog = optionPane.createDialog(source, "My Slider");
dialog.setVisible(true);
System.out.println ("Input: " + optionPane.getInputValue());
Figure 9-10. Using JOptionPane with a JSlider
Note If the user doesn't move the slider, JOptionPane.getInputValue() correctly returns
JOptionPane.UNINITIALIZED_VALUE .
Adding Components to the Button Area
In “The JOptionPane Options and Initial Value Arguments” section earlier in this chapter, you
saw that if you have a Component in the array of options for the JOptionPane , you must configure
the component yourself to handle selection. The same holds true for any components you add
via the options property. When a component is configured to handle selection, the pop-up
window that a JOptionPane is embedded in will disappear when the component is selected.
The default set of buttons works this way. When installing your own components, you must
notify the option pane when one of the components has been selected by setting the value
property of the option pane.
To demonstrate this mechanism, create a JButton with both an icon and a text label that
can be placed in an option pane. Without defining this component yourself, the option pane
supports only the display of a label or an icon on the button. When the button is selected, the
button tells the option pane it was selected by setting the option pane's value property to the
current text label of the button. Adding yet another method to OptionPaneUtils shown earlier
in Listing 9-1 allows you to create such a button. The boldfaced line in the source shown in
Listing 9-4 is the important method call to add to any other such component that you want to
combine with the component array for the options property of a JOptionPane . The line would
be called after selection of such a component.
 
Search WWH ::




Custom Search