Swing Components and Containers


JComponent Class

Most, but not all, Swing components inherit directly or indirectly from the JComponent class, which is an indirect subclass of the old AWT Component class. Since there are a few Swing components classes that do not inherit from JComponent, it is useful to divide the JComponent methods into two types: those that are inherited indirectly from the old AWT Component class, and those that are defined in the JComponent class itself.

Old AWT Methods
JComponent Methods

These methods can only be used with Swing components that inherit directly or indirectly from the JComponent class. The first three are used to get components sized correctly, but you need to know what the layout manager does with the sizes. See Swing Layout Management.

Container Class

The Container class is an old AWT class that extends the old AWT Component class. A Container object does not have any visual apperence of its own except as a background color for a region on the screen. Its importance stems from the fact that it can contain other components. It uses a layout manager to determine where its components are placed and how they are sized.

There are two kinds of containers in Swing: JPanel objects and content panes. Like most objects, JPanel objects are created using their class construcors. Content panes, on the other hand, are obtained by sending a getContentPane() message to a top-level window (a JApplet, JFrame, JWindow, JDialog, or JInternalFrame object).

Both JPanel objects and content panes indirectly inherit all of the old AWT Component methods. Since the JPanel class extends the JComponent class, its objects also inherit the JComponent methods.

In addition to the component methods, all containers inherit the container methods listed below.

The layout manager parameter in setLayout() is usually constructed on the spot with a new expression such as new FlowLayout(). The second version of add() is usually only needed with a BorderLayout. Its whr parameter then specifies which region the component should be placed in. See BorderLayout.

For those that must know the truth, the whole truth, and nothing but the truth, these methods are inherited from the old AWT Container class (which extends the old AWT Component class). In the case of content panes, the inheritance is direct. For the JPanel class, the inheritance is indirect through the JComponent class, which extends Container. This means that the Container methods could, in principle, be used with any JComponent. In practice, they should not be used except with content panes and JPanel objects.

Containers have default layout managers that control the layout of components in the container if you have not explicitly specified a layout manager. The default layout manager for JPanel objects is FlowLayout. The default layout manager for content panes is BorderLayout.

JApplet Class

The JApplet class is never used directly. Instead, you write a class that extends JApplet, providing an implementation of its init() method. When the applet appears in a browser window, the browser invokes the init() method. You write statements in the init method to construct and arrange the components and set up listeners to handle user keyboard and mouse actions.

JPanel Class

The JPanel class can be used either as a container for components, or as a blank area for drawing custom graphics. Only it use as a container is described here. For its use for custom graphics, see Swing Graphics.

When used as a container, a JPanel is constructed with one of the following constructors.

The default layout manager for a JPanel is a FlowLayout.

Any of the JComponent or Container methods can be used with a JPanel.

Simple Components

Several of the following components are demonstrated in the Components Demo Applet.

JLabel Class

A JLabel just displays text. It has no built-in response to user mouse or keyboard actions. In most common uses, you just create it and add it into a container. The most common constructor for a JLabel is the following.

If you want to change a label during program execution, you can send it a setText() message.

JButton Class

A JButton is useful when you want to provide the user with the capability of performing an action at any time. The button is constructed with a label that suggests the nature of the action.

The action performed by a JButton is defined in the actionPerformed() method of an action listener registered with the JButton.

JComboBox Class

JTextField Class

A JTextField is used to entry and display of short single-line text. The text that it initially displays can be specified as a constructor parameter. It can also be constructed with no initial text, with the text added later.

For user text entry, a JTextField usually responds to the user when the user hits the return key. The response to the user is implemented in the actionPerformed() method of an action listener registered with the JTextField. In the actionPerformed() method, you can obtain the text entered by the user with the getText() method.

For more information on using the JTextField class, see Sun's tutorial web page Using Text Components.

JTextArea Class

A JTextArea can be used to display and edit longer multiline text. You can construct a text area either with text specified by a constructor parameter, or you can start off with no text and add it later.

You can use the code setText("") to clear the text in a text area.

Unless you are doing something sophisticated, you will normally process the text in a text area after the user has taken some other action such as clicking on a button or selecting a menu item. The response would then be implemented in the actionPerformed() method of an action listener registered with the button or menu item. The text can be obtained with the getText() method.

For more information on using the JTextArea class, see Sun's tutorial web page Using Text Components.

JSlider Class

A JSlider is used to allow the user to select a value from a range of numerical values by moving a tab along a track. A JSlider is normall oriented horizontally, but can be oriented vertically with an extra constructor parameter. Other parameters provide the minimum, maximum, and starting values for the slider.

To respond to user actions with a JSlider, you create a ChangeListener, implementing the response in its StateChanged() method. The response usually uses the JSlider's getValue() method to get the current value of the slider.

JList Class

A JList displays a list of options that a user can select. Unlike a JComboBox, all of the options are displayed at all times.

You can do some very fancy things with a JList, such as dynamically changing the list of values that it presents. However, the most common use has fixed selection values. The easiest way to construct a JList with fixed selection values is to declare an initialized array of objects (usually strings) and pass it as the JList constructor argument.

Once a JList is constructed, you need to register a selection listener to respond to user selections. The response is implemented in the listener's valueChanged() method. In that method, you can determine the selected value by sending the JList a getSelectedValue() message.

For further information about the JList class see How to Use Lists.

Menu Classes

There are three main classes used in building graphical user interface menus: JMenuItem, JMenu, and JMenuBar. JMenuItem objects are added into a JMenu objects to form a menu. Several JMenu objects can be added into a JMenuBar to form a menu bar. The menu bar is then used as a parameter for the setJMenuBar() method of the JApplet or JFrame class.

JMenuItem Class

A JMenuItem is constructed in one of two ways.

For the first constructor, an ActionListener is later added to the JMenuItem. The parameter in the second constructor includes an ActionListener.

JMenu Class

A JMenu is a JMenuItem that can hold a group of JMenuItem objects.

JMenuItem objects are added to a JMenu with the add() method. Action objects can also be added to a JMenu. When this is done, a JMenuItem is created automatically.

The fact that JMenu is a subclass of JMenuItem means that a JMenu can be added to another JMenu. This allows you to construct nested menus.

JMenuBar Class

The JMenuBar class has a simple constructor.

Once you have constructed a JMenuBar, you then add JMenu objects to it.

The final step in constructing menus is adding a JMenuBar to either a JApplet or a JFrame with the setJMenuBar() method. The JMenuBar is passed as a parameter.