![]() ![]() ![]() |
16
Layout Managers
Features of the Layout Managers in JClass Elements
Interfaces
Properties
Constructors and Methods
Examples
16.1 Features of the Layout Managers in JClass Elements
This chapter describes JClass Elements' layout managers and the components that are closely associated with them. The layout managers are
JCAlignLayout
,JCColumnLayout
,JCElasticLayout
,JCGridLayout
, andJCRowLayout
.JCBorder
,JCBox
,JCBrace
, andJCSpring
are the associated components.
16.1.1 Layout Manager Classes
JCAlignLayout
JCAlignLayout
is a layout manager that provides a simple way to lay out a vertically arranged group of control components, each with an associated label (or other component) placed to its left.JCColumnLayout
JCColumnLayout
is a simple subclass ofJCElasticLayout
that allows layout in a single column.JCElasticLayout
JCElasticLayout
is a layout manager that supportsJCElastic
components either horizontally or vertically. A component is considered elastic if it either implements theJCElastic
interface or it has a constraint object that implements theJCElastic
interface. Layout is performed in either a single row or column (depending on its orientation when created). The preferred size is calculated in the direction of orientation. If the container is bigger than the preferred size of all the components then the extra space is divided up between the components that are "elastic" in the direction of the orientation. The extra space is allocated to each of the components with respect to their "elasticity". If all the elastic components have the same elasticity (in the direction of the orientation) then they are equally stretched. If there is an uneven number of pixels to apportion, then the first n units of elasticity are allocated the extra pixels, where n is the remainder when the total elasticity is divided by the number representing the extra pixels (n = total_elasticity mod extra_pixels).JCGridLayout
JCGridLayout
is an improved subclass ofGridLayout
. It lays out a grid of rows and columns based on the attributes of the individual rows and columns. WhereasGridLayout
uses the widest and tallest child to size each cell,JCGridLayout
uses the widest element in a column to set the width of that column, and the tallest element in a row to set the height of that row.JCRowLayout
JCRowLayout
is a simple subclass ofJCElasticLayout
that allows layout in a single row.
16.1.2 Associated Component Classes
JCBorder
JCBorder
can be used with any layout manager. With it you can place a border anywhere, not just around a component. You draw a border by overriding the component's paint method and callingJCBorder.draw()
. Its parameters allow you to specify theGraphics
object it will be passed, along with its border style, border size in pixels, placement of the top left corner relative to its parent, its width and height, and the shadow colors for its sides. Please refer to the API for a full description of the two variations of the parameter list for this method.Border styles may be any one of the following:
JCBox
JCBox
is a Swing container that uses theJCElasticLayout
to lay out components in a single row or column. Use theorientation
property within an IDE to control the orientation of the box. TheJCSpring
andJCBrace
components are useful Beans to use in conjunction with this container.JCBrace
An implementation of a component that participates in a layout even though it has no view. It is called a brace because its main function is to reserve space as a way of controlling the layout of the visible components. A brace usually has equal minimum and preferred sizes, and an unlimited maximum size.
JCSpring
This is a stretchable concrete implementation of the
JCElasticLayout
interface, which specifies components as stretchable for theJCElasticLayout
manager and its subclasses. AJCSpring
has independently settable elasticity parameters for both the horizontal and vertical directions.
16.2 Interfaces
JCElasticLayout
- The interface that informs enabled layout managers that a particular component should be stretched to its maximum before stretching any non-elastic components.
16.3 Properties
JCBox
JCBrace
JCSpring
16.4 Constructors and Methods
16.4.1 Layout Managers
JCAlignLayout
JCColumnLayout
A simple subclass of
JCElasticLayout
that arranges layout in a single column.
Creates a column layout that aligns components to the specified alignment:
SwingConstants.LEFT
,SwingConstants.CENTER
, orSwingConstants.RIGHT
JCElasticLayout
Use its constructors to provide the layout you want.
When adding an elastic constraint to an object, you can use one of these constants:
add(c, JCElasticLayout.HORIZONTALLY_ELASTIC_CONSTRAINT);
JCElasticLayout.HORIZONTALLY_ELASTIC_CONSTRAINT,
JCElasticLayout.VERTICALLY_ELASTIC_CONSTRAINT,
JCElasticLayout.COMPLETELY_ELASTIC_CONSTRAINT
JCGridLayout
Like
GridLayout
in the AWT,JCGridLayout
has a two-parameter constructor in which you specify the number of rows and columns for your grid, and a four-parameter version in which you specify horizontal and vertical gaps as well. Use this constructor just as you would aGridLayout
. Unlike the AWT'sGridLayout
,JCGridLayout
's rows may have different heights and its columns may have different widths. See the example later on in this chapter for a visual comparison between the two layout managers.
16.4.2 Associated Components
JCBox
JCBrace
JCSpring
16.5 Examples
JCGridLayout
The example shown here illustrates the difference between AWT's
GridLayout
and JClass Elements'JCGridLayout
, which conserves space by permitting rows to have different heights and columns to have different widths. The height of each row is determined by the height of the tallest component in that row, and the width of a column is determined by widest component in the column, independent of the width of other columns. WithJCGridLayout
, rows have varying heights and columns have varying widths.
Figure 49 : A comparison of JCGridLayout and GridLayout.
JCAlignLayout
This layout manager makes it easy to provide a vertical arrangement of data input fields and their associated labels. You can provide for more than two columns and, as the example shows, you aren't restricted to text fields.
Although its intended use is one with labels in the first column,
JCAlignLayout
lets you place any component in any column.Use it as you would any layout manager for a frame or panel:
JCAlignLayout layout = new JCAlignLayout(2, 5, 5);
setLayout(layout);
Figure 50 : Using JCAlignLayout.
![]() ![]() ![]() |