Tutorial 11 - Intermediate Swing
This tutorial explores many restricted entry Java Swing components (aka widgets). Basics|Advanced
Buttons
Buttons are often used to let the user start a process. They are created with the JButton() constructor and. Buttons can be deactivated with the setEnabled(false) method and tested with the isEnabled() method. setMnemonic(char) allows a hot key to be associated with the button. Buttons use an ActionEvent event listener that reacts to a button press. Many GUIs use an array set of buttons. Check Bgui for a demo of this technique.
Toggle buttons are a visual push on - push off mechanism. They are created with the JToggleButton() constructor. The isSelected() method returns the state of the button. In addition to ActionEvent, ChangeEvent and ItemEvent are triggered.
Checkboxes
Checkboxes allow restricted selection of options. More than one option (or none) may be selected. JCheckBox() is used to construct these widgets. A checkbox widget is normally associated with a JLabel caption but can also be created by using a constructor with a caption parameter. The state of a checkbox can be found with a simple if (boxObject.getSelectedObjects() != null) test.
Often there is no need for checkbox events as their states are checked when a process button is selected. But there are times you may wish to update dynamically as an checkbox item is changed. This requires a different interface called ItemListener which responds to change of state rather than mouse clicks. The required implementation method is itemStateChanged(). A simple two checkbox example follows:
NOTE: Layout managers are explained in a following tutorial.
Radio Buttons
Radio buttons allow restricted selection of alternatives. Only one radio button may be selected at a time. JRadioButton is used to construct these widgets. A radio button is normally associated with a JLabel caption but can also be created using a constructor with a caption parameter. The state of a radio button can be found with the isSelected() method.
Button groups are used to ensure the mutual exclusivity of buttons in the group (ie. only one can be on at a time). Use ButtonGroup() to construct a button group and then add buttons with the add() method.
Borders are normally added to button groups for a visual separation. Unfortunately this doesn't work in Java! You must create a pane for the group and then add a border to the pane. For example:
NOTE: Layout managers are explained in a following tutorial.
List Boxes, Combo Boxes and Spinners
List boxes allow display and selection from a group of items. They are created with the JList(object) constructor which can receive an array or a list model as a parameter. List boxes are often constructed within JScrollPane containers to allow scrolling of lists longer than the display area. MyList is a simple example of a list box.
Item selection can be altered with setSelectionMode(selectionMode). selectionMode can be set to one of the ListSelectionModel constants: MULTIPLE_INTERVAL_SELECTION, SINGLE__INTERVAL_SELECTION or SINGLE_SELECTION. A default selection can be set with setSelectedIndex(x). The first selected item is accessed with getSelectedValue() or getSelectedIndex(). Multiple selected items are accessed getSelectedValues() or getSelectedIndices() and are returned as arrays.
List models provide methods for management of the elements of a list. The DefaultListModel class contains the constructor DefaultListModel(array) and management methods such as addElement(objName), elementAt{index], removeElement(objName), removeAllElements() and removeElementAt(idx). Status check methods include contains(objName), indexOf(name) and size(). Conversion to an array uses copyInto(array).
List presentation can be altered with the setVisibleRowCount(x) method and the fOrientation (floatValue) property. floatValue can be set to VERTICAL, VERTICAL_WRAP or HORIZONTAL_WRAP.
Combo boxes are popup lists that also have a text field for adding a selection. Combo boxes are created with the JComboBox() constructor. MyCombo is a simple example of a combo box.
The method setEditable(true) makes combo box items editable. Selected items are accessed with the getSelectedItem() and getSelectedIndex() methods. Other useful methods are getItemAt(idx), getItemCount(), setSelectedItem(obj) and setMaximumRowCount(x).
Spinners are lists that have only one item visible at a time. Up and down arrows move through the list. No scroll bar is provided which minimizes the space occupied in the interface. The JSpinner() class uses a JFormattedText field for display and editing.
Bounded-Range Components
Bounded-range components are controls that have a single integer value within fixed integer boundaries. Examples of bounded-range controls are progress bars, sliders and scroll bars. Each bounded-range component has the following methods: setExtent(), setMaximum(), setMinimum(), setValue(), getValueIsAdjusting() and setOrientation(). Bounded-range components use ChangeEvent to start an update.
Progress bars indicate status for time consuming jobs. The basic JProgressBar class offers a subtle control. If you want a dialog frame for canceling an operation the classes, ProgressMonitor and ProgressMonitorInputStream are better choices. ProgressMonitorInputStream is a stream filter in addition to a progress monitor. ProgBar.java is a simple progress bar demo.
Sliders allow integer selection within limits. They can be dressed up with ticks and labels using setPaintTicks(true) and setPaintLabels(true). setMajorTickSpacing(int) and setMinorTickSpacing(int) set the intervals used for marker ticks. setSnapToTicks(true) forces the slider to the closest tick. setInverted(true) reverses the low and high marks. labelTable is a dictionary of slider values and JLabel objects for painting the object. Slider.java is a simple slider demo.
3rd Party Widgets
Although the Java SDK provides many commonly used IO widgets, some applications such as instrumentation require highly specialized widgets. JKnob shows how a custom widget can be added to your application as well as how to write a new io widget.
Tutorial Source Code
Obtain source for JKnob, MyCombo, MyList, MyTree and SimpleTable here.