JClass Elements

PreviousNextIndex

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, and JCRowLayout. JCBorder, JCBox, JCBrace, and JCSpring 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 of JCElasticLayout that allows layout in a single column.

JCElasticLayout

JCElasticLayout is a layout manager that supports JCElastic components either horizontally or vertically. A component is considered elastic if it either implements the JCElastic interface or it has a constraint object that implements the JCElastic 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 of GridLayout. It lays out a grid of rows and columns based on the attributes of the individual rows and columns. Whereas GridLayout 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 of JCElasticLayout 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 calling JCBorder.draw(). Its parameters allow you to specify the Graphics 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:

JCBorder.ETCHED_IN

Double line, border appears inset.

JCBorder.ETCHED_OUT

Double line, border appears raised.

JCBorder.FRAME_IN

1-pixel shadow-in at edge, border appears framed.

JCBorder.FRAME_OUT

1-pixel shadow-out at edge, border appears framed.

JCBorder.IN

Border appears inset.

JCBorder.OUT

Border appears raised.

JCBorder.CONTROL_IN

MS-Windows control shadows.

JCBorder.CONTROL_OUT

MS-Windows control shadows.

JCBorder.PLAIN

Border drawn in foreground color.

JCBorder.NONE

No border drawn.

JCBox

JCBox is a Swing container that uses the JCElasticLayout to lay out components in a single row or column. Use the orientation property within an IDE to control the orientation of the box. The JCSpring and JCBrace 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 the JCElasticLayout manager and its subclasses. A JCSpring 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

alignment

One of SwingConstants.LEFT, SwingConstants.CENTER, or SwingConstants.RIGHT, specifying the alignment of the layout.

orientation

One of JCElasticLayout.HORIZONTAL or JCElasticLayout.VERTICAL, specifying how the container is to lay out its components.

JCBrace

orientation

One of JCElasticLayout.HORIZONTAL or JCElasticLayout.VERTICAL, specifying whether it is a horizontal or a vertical brace.

length

This is the value of both the minimum size and the preferred size. Whether the length refers to a horizontal or a vertical dimension depends on the orientation.

JCSpring

horizontalElasticity
verticalElasticity

These are properties with integer values specifying the relative elasticities of the components to which they refer.


16.4 Constructors and Methods

16.4.1 Layout Managers

JCAlignLayout

getLabelVertical
  Alignment()

Returns the vertical position of a label relative to its control.

setResizeHeight()

Sets whether the control should be resized vertically to the height of the largest component in its row (default: false). This value is ignored for labels (the components in odd columns).

setResizeWidth()

Sets whether the control should be resized horizontally to its parent's right edge if it is in the last column (default: false).

setLabelVertical
  Alignment()

Sets the vertical position of a label relative to its control. Choices are TOP, MIDDLE (default), or BOTTOM.

JCColumnLayout

A simple subclass of JCElasticLayout that arranges layout in a single column.

JCColumnLayout()

Creates a column layout that aligns components on the left.

JCColumnLayout(
  int alignment)

Creates a column layout that aligns components to the specified alignment: SwingConstants.LEFT, SwingConstants.CENTER, or SwingConstants.RIGHT

JCElasticLayout

Use its constructors to provide the layout you want.

JCElasticLayout
  (int   orientation)

Creates a row layout that by default aligns components to the left of the row or column.

JCElasticLayout
  (int   orientation,   int alignment)

Creates a column layout that aligns components as specified by the second parameter, which can be one of the SwingConstants.LEFT, SwingConstants.RIGHT, SwingConstants.TOP, SwingConstants.BOTTOM, or SwingConstants.CENTER.

When adding an elastic constraint to an object, you can use one of these constants:

For example:

add(c, JCElasticLayout.HORIZONTALLY_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 a GridLayout. Unlike the AWT's GridLayout, 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

JCBox()

Creates a horizontal JCBox container. The constructor may have an optional parameter, int orientation. In this case, valid values are JCBox.HORIZONTAL or JCBox.VERTICAL.

createHorizontalBox()
createVerticalBox()

Convenience methods for creating JCBoxes.

getAlignment()
setAlignment(int   alignment)

Describes how the component is aligned. The alignment parameter is one of SwingConstants.LEFT, SwingConstants.CENTER, or SwingConstants.RIGHT.

getOrientation()
setOrientation(int   orientation)

The box uses the orientation to determine whether its components are arranged horizontally or vertically.

JCBrace

getOrientation()
setOrientation(int   orientation)

The parameter in the set method can be one of JCElasticLayout.HORIZONTAL or JCElasticLayout.VERTICAL, specifying how the container is to lay out its components.

getLength()
setLength(int length)

This is the value of both the minimum size and the preferred size. Whether the length refers to a horizontal or a vertical dimension depends on the orientation.

JCSpring

get/setHorizontal
  Elasticity()
get/setVertical
  Elasticity()

The set methods take an integer parameter specifying the relative elasticity of the JCSpring. When two or more springs are used, the elasticities are used as weighted values for the springiness.

get/setMaximumSize()
get/setMinimumSize()
get/setPreferredSize()

The set methods require a Dimension parameter, which the get methods return.


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. With JCGridLayout, 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.


PreviousNextIndex