Swing Containers : Intermediate-Level Containers

Intermediate-level containers contain Swing components, but themselves will be contained within one of the two top-level containers, applet or frame.

Panel

A panel is a grouping of Swing components. A GUI screen consists of any number of panels. Like all containers, a panel will have a layout type associated with it, this determines how the components within a panel are displayed. Panels differ from other containers in that a panel itself can contain subpanels as well as components.

The declaration

JPanel mypanel;

declares a mypanel object of type JPanel. The statement

mypanel = new JPanel();

creates the mypanel object using the JPanel() class constructor.

Scrollpane

Scrollpanes can be used to provide a scrollable view for any component whose size can change dynamically. Candidate components are text areas and lists, for example. A scrollpane will include horizontal and vertical scrollbars.

There are a number of constructors in the javax.swing.JScrollPane class. The simplest is JScrollPane(component). This will show scrollbars only if the contents of the component are larger than the scrollable view. So in a text area component, for example, scrollbars will be shown only when the user has filled the viewable text area with text. Another form of the constructor is JScrollPane(component, vertical_policy, horizontal_policy). vertical_ policy and horizontal_policy specify the behavior of the vertical and horizontal scrollbars, respectively. vertical_policy takes one of the following values:


JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED. This specifies that vertical scrollbars are shown only when needed.

JScrollPane.VERTICAL_SCROLLBAR_ALWAYS. This specifies that vertical scrollbars are always shown.

JScrollPane.VERTICAL_SCROLLBAR_NEVER.This specifies that vertical scrollbars are never shown.


horizontal_policy takes on similar values regarding horizontal scrollbar behavior. Once a scrollpane object is created, it is added to the content pane.

No comments:

Post a Comment