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
Button

A graphical push button.

Canvas

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

Checkbox

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.

CheckboxMenuItem

A toggle button that can appear within a Menu.

Choice

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.

Component

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

FileDialog

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

Label

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

List

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

Menu

A single pane of a pull-down menu

MenuBar

A horizontal bar that contains pull-down menus.

MenuComponent

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

MenuItem

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

PopUpMenu

A menu pane for a pop-up menu.

Scrollbar

A graphical scrollbar.

TextArea

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

TextComponent

The base class for both TextArea and TextField.

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
JButton

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

JCheckBox

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

JCheckBoxMenuItem

A checkbox designed for use in menus.

JColorChooser

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.

JComboBox

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.

JComponent

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

JEditorPane

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

JFileChooser

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.

JLabel

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

JList

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

JMenu

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

JMenuBar

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

JMenuItem

A selectable item within a menu.

JOptionPane

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

JPasswordField

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

JPopupMenu

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

JProgressBar

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

JRadioButton

A toggle button for displaying mutually exclusive choices.

JRadioButtonMenuItem

A radio button for use in menus.

JScrollBar

A horizontal or vertical scrollbar.

JSeparator

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

JSlider

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.

JTable

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.

JTextArea

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

JTextComponent

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

JTextField

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

JTextPane

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.

JToggleButton

The parent component of both JCheckBox and JRadioButton.

JToolBar

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

JToolTip

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

JTree

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.

This HTML Help has been published using the chm2web software.