Other Simple Swing Components

Note that all JComponents can have a Border (setBorder) and tool tips (setToolTipText).

1. JCheckBox

Similar to Checkbox (but note the capital B in JCheckBox). You can attach either an ActionListener or an ItemListener to monitor events. If you use an ActionListener, you'll want to call isSelected to distinguish a selection from a deselection. If you use an ItemListener, the ItemEvent itself has this information: call getStateChange and compare the result to ItemEvent.SELECTED or ItemEvent.DESELECTED. Unless you are sure that the background color of the JCheckBox matches the background color of the Container, you should call setContentAreaFilled(false). You can supply an icon to replace the normal square with a check in it (via setIcon), but if you do, be sure to also supply an icon to be displayed when the checkbox is selected (setSelectedIcon).

2. JRadioButton

A JRadioButton is somewhat similar to a Checkbox when the Checkbox is inside a CheckboxGroup. Create several JRadioButtons, add them to a ButtonGroup, and also add them to a Container. Like JCheckBox, you can attach either an ActionListener or an ItemListener. However, only the radio button that is clicked will get an ActionEvent, while both the one clicked and the one that becomes deselected as a result get an ItemEvent. Unless you are sure that the background color of the JRadioButton matches the background color of the Container, you should call setContentAreaFilled(false). You can supply an icon to replace the normal square with a check in it (via setIcon), but if you do, be sure to also supply an icon to be displayed when the checkbox is selected (setSelectedIcon).

3. JFileChooser

This control lets users interactively select a filename by browsing directories. Normal use involves allocating a JFileChooser (pass a String for the directory to the constructor, or "." to indicate the current directory, or leave it blank for home directory), setting a title via setDialogTitle, optionally specifying a default choice (setSelectedFile), optionally writing and attaching a FileFilter to limit the file types displayed, popping it up via showOpenDialog, passing in the parent Frame (this returns an int), then finally, if the int matches JFileChooser.APPROVE_OPTION (i.e. user didn't cancel), calling getSelectedFile. Note, however, that with the first two releases of Java 1.1 for Windows from Sun, the constructor call causes an error if used on a PC that has a removable drive (e.g. IOMega Zip Drive) with no disk in it. Sun promises this will be fixed in JDK 1.2.2.

4. JTextField

The basic use of this widget works almost exactly like the AWT's TextField, including size options to the constructor, ActionEvents on ENTER and TextEvents on regular keys. However, it does not play double duty as a password field; use JPasswordField instead. You can set the text alignment via setHorizontalAlignment; supply JTextField.LEFT, JTextField.CENTER, or JTextField.RIGHT.

5. JTextArea

This is very similar to the AWT TextArea, but two things should be noted. First, unlike TextArea it does not directly have scrolling behavior. Instead, like other Swing components, scrolling behavior is obtained by wrapping it in a JScrollPane. Second, JTextArea is only for simple text, but Swing also provides JTextPane and JEditorPane, which support much more complex options. In particular, JEditorPane discussed elsewhere in this tutorial can display HTML and RTF text.


This page is part of my Quick Swing Tutorial for AWT Programmers. © 1999 Marty Hall. All source code freely available for unrestricted use. Created for for work in the Research and Technology Development Center of the Johns Hopkins University Applied Physics Lab, for courses in the Johns Hopkins Part-Time MS Program in Computer Science, and for various industry seminars and Java short courses.