[an error occurred while processing this directive]
Domain for sale!
Start Search Contents Index Links About

Options Dialog Box

Using Swing GUI and Properties

2003 - Week 24 - Havard Rast Blok

JTabbedPane from the Swing library lets you easily construct complex options dialog boxes containing many different classes of options. This introduction will look at the basics of JTabbedPane in a JDialog box and then focus on some generic features of option dialog boxes. Finally, we see how the values of the GUI components may be extracted and stored in a Properties object for convenience access in other parts of you application.

First we will need a small application to work with, or at least a dialog box. Since we are not implementing any other logic into the GUI, this example shows nothing else than the JTabbedPane dialog box. The components included are a JComboBox, JSpinner, JCheckBox and JSlider in addition to the labels.

As seen from the snapshots below, the options dialog box is for some imaginary game where the user may choose the speed and rules of the games, and some details of the file saving.

This example can be downloaded here:


OptionsDialog.java

Now that we have our GUI, we would need to extract the settings from the components. This could be done in different ways at various times, like reading all components at exit or only the modified ones. We will look at the last approach, where changes are monitored through registered listeners.

For the JCheckBox, JSpinner and JSlider we can use the ChangeListener interface to detect changes to the component, while the JComboBox requires the ActionListener (the ItemListener may also be used). So far we will not add any logic to the methods of the interfaces; they will merely print the captured events to screen. You can see the modified code below.

Note: The ActionListener for the JComboBox only captures changes by the spinner arrows and not when the user types in numbers in the text field. To get these KeyEvents as well, consult the Java Doc and study the editor framework for the JSpinner.


OptionsDialog.java

Once the events of the components are captured, we may extract the new information from them. Again, this can be done in hundred and one ways, and for this example I chose a generic method that tracks the updated value by locking at the class type for the source of the event.

The second change to the code is the names that are given to the components, by the setName() method found in java.awt.Component. This is meant to be a unique name that that identifies the component, and extracted together with the value. The code for this is shown below, along with the updated class.

    Object src;
    String value, name;

    src = e.getSource();
    name = ((Component)src).getName();
    value="";

    if( src instanceof AbstractButton )
    {
      value = ""+((AbstractButton)src).isSelected();
    }
    else if( src instanceof JSlider )
    {
      value = ""+((JSlider)src).getValue();
    }
    else if( src instanceof JSpinner )
    {
      value = ""+((JSpinner)src).getValue();
    }
    else if( src instanceof JComboBox )
    {
      value = ""+((JComboBox)src).getSelectedIndex();
    }

    


OptionsDialog.java

The final step is now to store the values somewhere outside the dialog box. In a Properties object is on convenient way, now that we have both names and values as strings. This class is easy to use, and even features methods for reading and writing the settings to file. The current code only writes the values to screen when the OK button is pressed, as the method call below shows.

    props.store( System.out, "Settings from OptionsDilog" );
    


OptionsDialog.java

Now that we have a generic framework for handling the values of the options dialog box, a good way to further enhance the dialog box class is to couple it with the Field Value Loader which will set or get the fields of any object based on a Properties object. It uses the powerful Java reflection to handle any variable type, even primitive types.



site: Håvard Rast Blok
mail:
updated: 16 July 2010