Represents an alignment of a component (or group) within a parallel group.
Represents an alignment of a component (or group) within a parallel group.
javax.swing.GroupLayout.Alignment
Allows to specify whether to anchor the baseline to the top or the bottom of a baseline-aligned parallel group.
Allows to specify whether to anchor the baseline to the top or the bottom of a baseline-aligned parallel group.
Wraps an arbitrary component so that it may appear within a group of
type A
.
Wraps an arbitrary component so that it may appear within a group of
type A
.
Wraps a GUI component so that it may appear in a parallel group.
Wraps a GUI component so that it may appear in a parallel group.
javax.swing.GroupLayout.ParallelGroup
Wraps a GUI component so that it may appear in a sequential group.
Wraps a GUI component so that it may appear in a sequential group.
javax.swing.GroupLayout.SequentialGroup
Wraps an arbitrary component to allow for custom size constraints.
Wraps an arbitrary component to allow for custom size constraints.
Pixel size and Infinite
.
Pixel size and Infinite
. Used to specify the size of a Gap
.
A group of components, either parallel or sequential.
A group of components, either parallel or sequential.
Wraps a group of components to extend it with actions only allowed inside a parallel group.
Wraps a group of components to extend it with actions only allowed inside a parallel group.
javax.swing.GroupLayout.ParallelGroup
Wraps a group of components to extend it with actions only allowed inside a sequential group.
Wraps a group of components to extend it with actions only allowed inside a sequential group.
javax.swing.GroupLayout.SequentialGroup
Elements with this trait may only appear in groups corresponding to the
type A
.
Elements with this trait may only appear in groups corresponding to the
type A
.
the type of the group: sequential, parallel or both
Specifies how two components are placed relative to each other.
Specifies how two components are placed relative to each other.
javax.swing.LayoutStyle.ComponentPlacement
Pixel size, UseDefault
and Infinite
.
Pixel size, UseDefault
and Infinite
. Used to specify the preffered
size of PreferredGap
s and ContainerGap
s.
Specifies if two components are related or not.
Specifies if two components are related or not.
javax.swing.LayoutStyle.ComponentPlacement
Allows to specify whether a parallel group should be resizable or of fixed size.
Allows to specify whether a parallel group should be resizable or of fixed size.
Pixel size and all size hints.
Pixel size and all size hints. Used to specify sizes of a component and
the maximum size of PreferredGap
s and ContainerGap
s.
Anchor the baseline to the bottom of the group.
Anchor the baseline to the bottom of the group.
Anchor the baseline to the top of the group.
Anchor the baseline to the top of the group.
Elements are aligned along their baseline.
Elements are aligned along their baseline. Only valid along the vertical axis.
Elements are centered inside the group.
Elements are centered inside the group.
A manual gap between a component and a container (or group) edge.
A manual gap between a component and a container (or group) edge. Only valid inside a sequential group.
A gap between a component and a container (or group) edge that acts like a spring, pushing the element away from the edge.
A gap between a component and a container (or group) edge that acts like a spring, pushing the element away from the edge.
The corresponding parallel group should be of fixed size.
The corresponding parallel group should be of fixed size.
A manual gap between two components.
A manual gap between two components.
Used to request the (horizontal) indentation of a component that is positioned underneath another component.
Used to request the (horizontal) indentation of a component that is positioned underneath another component.
Represents an arbitrarily large size.
Represents an arbitrarily large size.
Elements are anchored to the leading edge (origin) of the group.
Elements are anchored to the leading edge (origin) of the group.
Declares a parallel group, i.e.
Declares a parallel group, i.e. a group whose child components share the same space. Because some components may (and will) be bigger than others, an alignment can be specified to resolve placement of the components relative to each other.
GroupLayout.ParallelGroup
A gap between two components with a size that is defined by the
LayoutStyle
used, depending on the relationship between
the components.
A gap between two components with a size that is defined by the
LayoutStyle
used, depending on the relationship between
the components.
Used to request the distance between two visually related components.
Used to request the distance between two visually related components.
The corresponding parallel group should be resizable.
The corresponding parallel group should be resizable.
Declares a sequential group, i.e.
Declares a sequential group, i.e. a group whose child components appear one after another within the available space.
GroupLayout.SequentialGroup
A gap between two components that acts like a spring, pushing the two elements away from each other.
A gap between two components that acts like a spring, pushing the two elements away from each other.
Elements are anchored to the trailing edge (end) of the group.
Elements are anchored to the trailing edge (end) of the group.
Used to request the distance between two visually unrelated components.
Used to request the distance between two visually unrelated components.
Instructs the layout to use a component's default size.
Instructs the layout to use a component's default size.
Instructs the layout to use a component's preferred size.
Instructs the layout to use a component's preferred size.
Implicit conversion that puts a component into the correct context on demand.
Implicit conversion that puts a component into the correct context on demand.
Indicates whether gaps between components and the container borders are automatically created.
Indicates whether gaps between components and the container borders are automatically created.
Sets whether gaps between components and the container borders are automatically created.
Sets whether gaps between components and the container borders are automatically created.
Indicates whether gaps between components are automatically created.
Indicates whether gaps between components are automatically created.
Sets whether gaps between components are automatically created.
Sets whether gaps between components are automatically created.
Implicit conversion that puts a group into the correct context on demand.
Implicit conversion that puts a group into the correct context on demand.
Implicit conversion that puts a group into the correct context on demand.
Implicit conversion that puts a group into the correct context on demand.
The component will not take up any space when it's invisible (default).
The component will not take up any space when it's invisible (default).
Indicates whether the visibilty of components is considered for the layout.
Indicates whether the visibilty of components is considered for the layout.
If set to false
, invisible components still take up space.
Defaults to true
.
Sets whether the visibilty of components should be considered for the layout.
Sets whether the visibilty of components should be considered for the
layout. If set to false
, invisible components still take up
space. Defaults to true
.
The component will still take up its space even when invisible.
The component will still take up its space even when invisible.
Implicitly converts an Int to a Size object when needed.
Implicitly converts an Int to a Size object when needed.
a size in pixels; must be >= 0
This panel's underlying layout manager.
This panel's underlying layout manager.
Returns the layout style used.
Returns the layout style used.
Assigns a layout style to use.
Assigns a layout style to use.
Links the sizes of several components horizontally.
Links the sizes of several components horizontally.
the components to link
Links the sizes (horizontal and vertical) of several components.
Links the sizes (horizontal and vertical) of several components.
the components to link
Links the sizes of several components vertically.
Links the sizes of several components vertically.
the components to link
The swing JPanel
wrapped by this GroupPanel
.
The swing JPanel
wrapped by this GroupPanel
.
Replaces one component with another.
Replaces one component with another. Great for dynamic layouts.
the component to be replaced
the component replacing the existing one
Starting point for the horizontal layout.
Starting point for the vertical layout.
A panel that uses javax.swing.GroupLayout to visually arrange its components.
The key point to understanding this layout manager is that it separates horizontal and vertical layout. Thus, every component appears twice: once in the horizontal and once in the vertical layout. Consult the Java API documentation for
GroupLayout
and Sun's Java tutorials for a comprehensive explanation.The main advantage of using this panel instead of manually tinkering with the layout is that this panel provides a concise, declarative syntax for laying out its components. This approach should make most use cases easier. In some special cases, e.g. when re-creating layouts on-the-fly, it might be preferable to use a more imperative style, for which direct access to the underlying layout manager is provided.
In contrast to the underlying swing layout, this panel activates the automatic creation of gaps between components by default, since this panel is intended for coding UIs "by hand", not so much for visual UI builder tools. Many features of the underlying layout are aimed at those, tough. Most of them are available through this panel for completeness' sake but it is anticipated that coders won't need to use them very much.
Code examples
This section contains a few simple examples to showcase the basic functionality of
GroupPanel
s. For all examples, it is assumed that everything from the packagescala.swing
is imported and the code is placed inside a scala.swing.SimpleSwingApplication like this:Simple panel with 2 components
In the first example, there's a label and a text field, which appear in a horizontal sequence but share the same vertical space.
It can be observed that the resize behaviour of the text field is rather strange. To get better behaviour, the components' vertical sizes can be linked together.
Alternatively, it would have been possible to disallow the resizing of the vertical, parallel group. To achieve this, the vertical layout line should be written this way:
Since text fields aren't resizable when used with baseline alignment (more about that further down), the following code also prevents (vertical) resizing:
Size and alignment
Components can be added with custom size constraints (minumum, preferred, maximum size). The next example showcases that. The text field appears with a preferred height of 100 pixels and when the component is resized, it can be shrinked down to its minimum height of 50 pixels and enlarged to its maximum height of 200 pixels.
Note: similar to the methods
asBaseline
andaligned
, thesized(...)
method is made available by an implicit conversion. But because type inference does not work well with implicits in this case, the conversion (via theadd
) method must be done explicitly. The same holds for all the sizing methods for components.The vigilant reader may have noticed that the
sized(...)
method does not take integer parameters but rather instances of the typeSize
. This type, along with some specializations of it, are used to enforce valid values for sizes, which can be pixel sizes greater than or equal to zero or special hints:UseDefault
,UsePreferred
andInfinite
. For convenience,Int
s are implicitly converted to pixel sizes where required.Instead of using these hints, it most of the time is easier to use the provided convenience methods:
sized
,fixedToDefaultSize
,resizable
andfullyResizable
.Because the default alignment in a parallel group is
Leading
, both components are "glued" to the top of the container (panel). To align the label's text with the text inside the text field, an explicit alignment can be specified in a preceding argument list, like this:This example also shows a potential problem of baseline alignment: some components stop being resizable. More specifically, the javadoc for
GroupLayout.ParallelGroup
states:CONSTANT_ASCENT
orCONSTANT_DESCENT
.OTHER
orCENTER_OFFSET
are not resizable.Since a text field's resizing behaviour is
CENTER_OFFSET
, it is not resizable when used with baseline alignment.Gaps
The
GroupPanel
turns on automatic creation of gaps between components and along the container edges. To see the difference, try turning this feature off manually by inserting the following lines:With both types of gaps missing, the components are clamped together and to the container edges, which does not look very pleasing. Gaps can be added manually, too. The following example does this in order to get a result that looks similar to the version with automatically created gaps, albeit in a much more verbose manner.
Rigid gaps with custom size or completely manual gaps (specifying minimum, preferred and maximum size) between components are created with the
Gap
object:In a parallel group, such a gap can be used to specify a minimum amount of space taken by the group.
In addition to rigid gaps in the previous example, it is also possible to specify gaps that resize. This could be done by specifying a maximum size of
Infinite
. However, for the most commonly used type of these, there is a bit of syntax sugar available with theSpring
andContainerSpring
objects.bc.. theHorizontalLayout is Sequential( ContainerGap(), label, Spring(), // default is Related textField, ContainerSpring() )
These create gaps that minimally are as wide as a
PreferredGap
would be - it is possible to specify whether theRelated
orUnrelated
distance should be used - but can be resized to an arbitrary size.The preferred size can also be specified more closely (
UseDefault
orInfinite
aka "as large as possible"):Please note that
PreferredGap
,Spring
,ContainerGap
andContainerSpring
may only be used inside a sequential group.A dialog with several components
As a last, more sophisticated example, here's the
GroupPanel
version of the "Find" dialog presented as example forGroupLayout
in the Java tutorials by Sun:javax.swing.GroupLayout