This web presentation is intended to provide quick access to a large amount of information about Swing graphical user interface visual components. Information that is needed frequently is contained directly in this presentation. In addition it contains embedded tutorial and API content from Oracle's web site.
All Swing components (including containers) inherit from the JComponent class. Although there are well over 100 methods defined in the JComponent class, and many more inherited from its ancestor classes, there are only 9 methods that are used in most applications. Swing components are frequently made more user-friendly, sized, or styled with JComponent methods. Those methods are briefly described here.
Each component described here is presented as an applet along with actual code for the component. The applet shows the component and information about its state. The state information is updated in response to user interaction with the the component. This is done within a framework that puts all component code into three methods:
The most commonly used JComponent methods are used for user friendliness, sizing, or styling.
Most components other than containers have their size set automatically. Some containers, notably scroll panes, have a default size of 0×0. Then you will definitely want to set it size. In most cases only the preferred size needs to be set.
The Swing components in this web presentation are displayed by the Components applet. This applet, along with a Containers applet for displaying Swing containers, use a display framework designed to emphasize the code that is commonly used for setting up components or containers.
Both applets are parameterized. A web page uses an <object> tag to include the applet. This tag can have nested <param> tags to pass parameters to the applet.
The components and containers applets each have a single parameters. If it is included it specifies the type of component or container. If the parameter is omitted the applets display a combo box to let the user make the selection.
The display framework has 3 classes:
GUI application use cases generally have 4 aspects:
Most of the Swing components get information from the user. They also have a built-in capability for storing that information as part of their internal state.
All of the Swing components have a built-in capability of presenting information to the user. If for no other reason, it is essential for providing feedback to the user. For example, a check box displays a check mark or not, depending on its current state.
Triggering an action is under the control of the programmer. If a programmer wants to use a component as an action trigger the can install a listener on the component.
When to trigger an action is an important decision in implementing a use case. Some times it is best to do so immediately whenever the user performs an action. For more complicated use cases it is usually better to defer triggering an action until all necessary information has been collected.
For complicated use cases, there is a universal action trigger component: a button. The user enters data in several components, then clicks a button. A "Submit" button is a common illustration of this pattern.
Most other components can act as an action triggers if more immediate action is needed.
Check boxes and toggle buttons are two alternatives for user entry of boolean, "yes"-"no", or "on"-"off" data. Check boxes are more familiar to users and also more compact.
The three easy-to-use components for string input are text fields, password fields, and text areas.
All of these text components have a document object that maintains the state information. The listeners in the text and password fields are action listeners that are registered directly with the component. They are triggered when the user hits the return key.
The listener for the text area is a document listener. It is registered with the document object. It is triggered whenever the text changes.
If you need a text field but you want to respond immediately to all text changes then you can register a document listener with the document as in the text area. This does not make sense for a password field but it could be done.
Swing has two components for entering integer data.
Both of the components have limited precision. The precision of sliders is limited to about 3 digits of accuracy by the pixel resolution of the display. You can achieve higher precision with a spinner but if you do it will take the user a long time to "spin" to the desired value.
For high precision integer input it is better to use a text field.
The text input can be converted to an integer using the static
Integer.parseInt(String str)
method.
This method throws a NumberFormatException if str does not
contain a parsable integer.
A try-catch statement can catch this exception to change the background
color of the text field as an error indication.
Swing has two types of components for displaying structured data:
Configuring trees is a bit complicated so they are not described here. Instead, tutorial information from Oracle's web site is included
Selectors allow the user to choose one of several objects. Swing offers combo boxes, lists, and radio button for this purpose. Combo boxes and lists have been converted into generic classes so they can be used with any type of object. Radio buttons can be used for selection but the coding is a bit awkward.
Combo boxes are more compact, showing the options only when the user clicks on the combo box. Lists are desirable when display space is not too crowded and you want the user to always be able to see all options.