

Book Home Java Enterprise in a Nutshell Search this book

2.2. Components

A graphical user interface is composed of individual building blocks such as push buttons, scrollbars, and pull-down menus. Some programmers know these individual building blocks as controls, while others call them widgets. In Java, they are typically called components because they all inherit from the base class java.awt.Component.

When you are describing a GUI toolkit, one of the most important characteristics is the list of components it supports. Table 2-1 lists the heavyweight components provided by AWT, where heavyweight refers to components that are layered on top of native GUI components. The components listed are all classes in the java.awt package. One of the curious features of the AWT is that pull-down and pop-up menus, and the items contained within those menus, are not technically components. Instead of inheriting from Component, they inherit from java.awt. MenuComponent. Nevertheless, the various menu component classes are used in very much the same way that true components are, so I have included them in Table 2-1.

Table 2-1. Heavyweight AWT Components

Component Name Description

A graphical push button.


A heavyweight component that displays a blank canvas, allowing a program to display custom graphics.


A toggle button that can be selected or unselected. Use the Checkbox group to enforce mutually exclusive or radio button behavior among a group of Checkbox components.


A toggle button that can appear within a Menu.


An option menu or drop-down list. Displays a menu of options when clicked on and allows the user to select among this fixed set of options.


The base class for all AWT and Swing components. Defines many basic methods inherited by all components.


Allows the user to browse the filesystem and select or enter a filename.


Displays a single line of read-only text. Does not respond to user input in any way.


Displays a list of choices (optionally scrollable) to the user and allows the user to select one or more of them.


A single pane of a pull-down menu


A horizontal bar that contains pull-down menus.


The base class from which all menu-related classes inherit.


A single item within a pull-down or pop-up menu pane.


A menu pane for a pop-up menu.


A graphical scrollbar.


Displays multiple lines of plain text and allows the user to edit the text.


The base class for both TextArea and TextField.


Displays a single line of plain text and allows the user to edit the text.

Table 2-2 lists the components provided by Swing. By convention, the names of these components all begin with the letter J. You'll notice that except for this J prefix, many Swing components have the same names as AWT components. These are designed to be replacements for the corresponding AWT components. For example, the lightweight Swing components JButton and JTextField replace the heavyweight AWT components Button and TextField. In addition, Swing defines a number of components, some quite powerful, that are simply not available in AWT.

Swing components are all part of the javax.swing package. Despite the javax package prefix, Swing is a core part of the Java 2 platform, not a standard extension. Swing can be used as an extension to Java 1.1, however. All Swing components inherit from the javax.swing.JComponent class. JComponent itself inherits from the java.awt.Component class, which means that all Swing components are also AWT components. Unlike most AWT components, however, Swing components do not have a native "peer" object and are therefore "lightweight" components, at least when compared to the AWT components they replace. Finally, note that menus and menu components are no different than any other type of component in Swing; they do not form a distinct class hierarchy as they do in AWT.

Table 2-2. GUI Components of Swing

Component Name Description

A push button that can display text, images, or both.


A toggle button for displaying choices that are not mutually exclusive.


A checkbox designed for use in menus.


A complex, customizable component that allows the user to select a color from one or more color spaces. Used in conjunction with the javax.swing.colorchooser package.


A combination of a text entry field and a drop-down list of choices. The user can type a selection or choose one from the list.


The root of the Swing component hierarchy. Adds Swing-specific features such as tooltips and support for double-buffering.


A powerful text editor, customizable via an EditorKit object. Predefined editor kits exist for displaying and editing HTML- and RTF-format text.


A complex component that allows the user to select a file or directory. Supports filtering and optional file previews. Used in conjunction with the javax.swing.filechooser package.


A simple component that displays text, an image, or both. Does not respond to input.


A component that displays a selectable list of choices. The choices are usually strings or images, but arbitrary objects may also be displayed.


A pull-down menu in a JMenuBar or a submenu within another menu.


A component that displays a set of pull-down menus.


A selectable item within a menu.


A complex component suitable for displaying simple dialog boxes. Defines useful static methods for displaying common dialog types.


A text input field for sensitive data, such as passwords. For security, does not display the text as it is typed.


A window that pops up to display a menu. Used by JMenu and for standalone pop-up menus.


A component that displays the progress of a time-consuming operation.


A toggle button for displaying mutually exclusive choices.


A radio button for use in menus.


A horizontal or vertical scrollbar.


A simple component that draws a horizontal or vertical line. Used to visually divide complex interfaces into sections.


A component that simulates a slider control like those found on stereo equalizers. Allows the user to select a numeric value by dragging a knob. Can display tick marks and labels.


A complex and powerful component for displaying tables and editing their contents. Typically used to display strings but may be customized to display any type of data. Used in conjunction with the javax.swing.table package.


A component for displaying and editing multiple lines of plain text. Based on JTextComponent.


The root component of a powerful and highly customizable text display and editing system. Part of the javax.swing.text package.


A component for the display, input, and editing of a single line of plain text. Based on JTextComponent.


A subclass of JEditorPane for displaying and editing formatted text that is not in HTML or RTF format. Suitable for adding simple word processing functionality to an application.


The parent component of both JCheckBox and JRadioButton.


A component that displays a set of user-selectable tools or actions.


A lightweight pop-up window that displays simple documentation or tips when the mouse pointer lingers over a component.


A powerful component for the display of tree-structured data. Data values are typically strings, but the component can be customized to display any kind of data. Used in conjunction with the javax.swing.tree package.

Library Navigation Links

Copyright © 2001 O'Reilly & Associates. All rights reserved.