|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectbb.gui.LineLayout
public class LineLayout
This layout manager is intended to offer similar functionality as BoxLayout
.
It was written solely to fix bugs/bad behavior
in BoxLayout.
Because of this class's deep similarity with BoxLayout, the following description, which was lifted from the javadocs of BoxLayout and customized, applies here:
LineLayout is a layout manager that allows multiple Components to be layed out either vertically or horizontally. The Components will not wrap so, for example, a vertical arrangement of Components will stay vertically arranged when the frame is resized.When you create a LineLayout, you specify whether its major axis is the
x-axis
(i.e. left to right placement) ory-axis
(i.e. top to bottom placement). Components are arranged from left to right (or top to bottom), in the same order as they were added to the Container. Invisible Components are ignored (they never affect the layout).For left to right layout, Components start to the right of the left edge, and are laid out edge-edge, heading right, in the order in which they were added, at their preferred sizes. Each Component's
y-alignment
is used to determine where it sits above or below the horizontal line going thru the middle of the target Container. (Itsx-alignment
is ignored.)For top to bottom layout, Components start below the top edge, and are laid out edge-edge, heading to the bottom, in the order in which they were added, at their preferred sizes. Each Component's
x-alignment
is used to determine where it sits left or right of the vertical line going down the middle of the target Container. (Itsy-alignment
is ignored.)Nesting multiple panels with different combinations of horizontal and vertical layout gives an effect similar to
GridBagLayout
, without the complexity.Instead of using LineLayout directly, programs may find it convenient to use the
LinePanel
class. LinePanel is a lightweight Swing container that initially uses a LineLayout. LinePanel also provides handy strut methods to help you use LineLayout well.
In terms of the API exposed by this class, it is identical to BoxLayout with one exception:
its sole constructor
only requires specification of the alignment axis.
Moving beyond method signatures to method semantics, this class is mostly identical to BoxLayout with the following exceptions:
works inconsistently
with alignment values.
Consequently, the only way to achieve justification with it is to use barbaric techniques
such as embedding Components inside Panels, and placing Glue/Strut Components to the sides.)
Other than the LineLayout.UnitTest
inner class of this class, there is no Swing-specific functionality in this class.
Thus, this LayoutManager may be used even in all-AWT programs.
Like typical Java GUI code, this class is not multithread safe:
it expects to only be called by EventQueue
's dispatch thread
.
This threading limitation is checked in every public method.
Nested Class Summary | |
---|---|
static class |
LineLayout.UnitTest
See the Overview page of the project's javadocs for a general description of this unit test class. |
Field Summary | |
---|---|
private int |
axis
Stores which axis to align along (i.e. the tiled dimension). |
private Container |
firstTarget
Stores the first Container to use this instance (only one may ever use it). |
private static long |
serialVersionUID
|
private Dimension |
totalMaximumSize
Caches the value returned by maximumLayoutSize . |
private Dimension |
totalMinimumSize
Caches the value returned by minimumLayoutSize . |
private Dimension |
totalPreferredSize
Caches the value returned by preferredLayoutSize . |
private boolean |
validatedStatus
Records whether or not this LayoutManager has been validated against the current version of the target Container. |
static int |
xAxis
Specifies that Components should be laid out left to right. |
static int |
yAxis
Specifies that Components should be laid out top to bottom. |
Constructor Summary | |
---|---|
LineLayout(int axis)
Creates a layout manager that will lay out Components in a line either left to right or top to bottom, as specified in the axis argument. |
Method Summary | |
---|---|
void |
addLayoutComponent(Component comp,
Object constraints)
Not used by this class (is implemented as an empty method). |
void |
addLayoutComponent(String name,
Component comp)
Not used by this class (is implemented as an empty method). |
protected int |
calcXOffset(Component c,
Dimension allocSize,
Insets insets)
Calculates the x-offset of a Component, taking into effect both the x-axis alignment of the Component and the Insets of the parent Container. |
protected int |
calcYOffset(Component c,
Dimension allocSize,
Insets insets)
Calculates the y-offset of a Component, taking into effect both the y-axis alignment of the Component and the Insets of the parent Container. |
protected void |
checkContainerArgAndThread(Container target)
Confirms that target is the same Container that this LineLayout instance was first used with. |
protected Dimension |
decreaseSizeForInsets(Dimension size,
Insets insets)
Returns a new Dimension instance that comes from decreasing the supplied Dimension arg to account for the target Container's Insets. |
float |
getLayoutAlignmentX(Container target)
Returns the alignment along the x axis. |
float |
getLayoutAlignmentY(Container target)
Returns the alignment along the y axis. |
protected void |
increaseSizeForInsets(Dimension size,
Container target)
Increases the supplied Dimension arg to account for the target Container's Insets. |
void |
invalidateLayout(Container target)
Indicates that a child has changed its layout related information, and thus any cached calculations should be flushed. |
void |
layoutContainer(Container target)
Called by the AWT when the specified Container needs to be laid out. |
static LineLayout |
makeHorizontal()
Simply returns new LineLayout(xAxis) . |
static LineLayout |
makeVertical()
Simply returns new LineLayout(yAxis) . |
Dimension |
maximumLayoutSize(Container target)
Returns the maximum dimensions needed to lay out the Components contained in the specified target container. |
Dimension |
minimumLayoutSize(Container target)
Returns the minimum dimensions needed to lay out the Components contained in the specified target container. |
Dimension |
preferredLayoutSize(Container target)
Returns the preferred dimensions needed to lay out the Components contained in the specified target container. |
void |
removeLayoutComponent(Component comp)
Not used by this class (is implemented as an empty method). |
protected void |
revalidateLayout(Container target)
|
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
private static final long serialVersionUID
public static final int xAxis
public static final int yAxis
private final int axis
private Container firstTarget
private final Dimension totalMinimumSize
minimumLayoutSize
.
Calculated inside revalidateLayout
as follows:
xAxis
,
yAxis
private final Dimension totalPreferredSize
preferredLayoutSize
.
Calculated inside revalidateLayout
as follows:
xAxis
,
yAxis
private final Dimension totalMaximumSize
maximumLayoutSize
.
Calculated inside revalidateLayout
as follows:
xAxis
,
yAxis
private boolean validatedStatus
invalidateLayout
,
revalidateLayout
Constructor Detail |
---|
public LineLayout(int axis) throws IllegalArgumentException, IllegalStateException
Note: unlike the corresponding BoxLayout constructor, the LineLayout constructor does not take a Container arg. Instead, the target Container is more conveniently established by "lazy initialization". Specificly, the first Container arg that is presented to one of the LayoutManager or LayoutManager2 methods of this class will establish what target Container this instance is for.
This elimination of the Container arg allows for a convenient compaction of code. Compare:
JPanel jpanel = new JPanel( new LineLayout(LineLayout.xAxis) );
versus
JPanel jpanel = new JPanel();
jpanel.setLayout( new BoxLayout(jpanel, BoxLayout.xAxis) );
(Even more convenient are the methods makeHorizontal
and makeVertical
.)
axis
- the axis to lay out Components along.
For left-to-right layout, specify LineLayout.xAxis
;
for top-to-bottom layout, specify LineLayout.yAxis
IllegalArgumentException
- if the axis arg has an invalid value
IllegalStateException
- if calling thread is not EventQueue
's dispatch thread
Method Detail |
---|
public static LineLayout makeHorizontal() throws IllegalStateException
new LineLayout(xAxis)
.
Example use: JPanel jpanel = new JPanel( LineLayout.makeHorizontal() );
IllegalStateException
- if calling thread is not EventQueue
's dispatch thread
public static LineLayout makeVertical() throws IllegalStateException
new LineLayout(yAxis)
.
Example use: JPanel jpanel = new JPanel( LineLayout.makeVertical() );
IllegalStateException
- if calling thread is not EventQueue
's dispatch thread
public float getLayoutAlignmentX(Container target) throws AWTError, IllegalStateException
getLayoutAlignmentX
in interface LayoutManager2
target
- a Container
AWTError
- if checkContainerArgAndThread
objects to container
IllegalStateException
- if calling thread is not EventQueue
's dispatch thread
public float getLayoutAlignmentY(Container target) throws AWTError, IllegalStateException
getLayoutAlignmentY
in interface LayoutManager2
target
- a Container
AWTError
- if checkContainerArgAndThread
objects to container
IllegalStateException
- if calling thread is not EventQueue
's dispatch thread
public Dimension minimumLayoutSize(Container target) throws AWTError, IllegalStateException
minimumLayoutSize
in interface LayoutManager
target
- the Container that needs to be laid out
AWTError
- if checkContainerArgAndThread
objects to container
IllegalStateException
- if calling thread is not EventQueue
's dispatch thread
preferredLayoutSize
,
maximumLayoutSize
public Dimension preferredLayoutSize(Container target) throws AWTError, IllegalStateException
preferredLayoutSize
in interface LayoutManager
target
- the Container that needs to be laid out
IllegalStateException
- if calling thread is not EventQueue
's dispatch thread
AWTError
- if checkContainerArgAndThread
objects to containerminimumLayoutSize
,
maximumLayoutSize
public Dimension maximumLayoutSize(Container target) throws AWTError, IllegalStateException
maximumLayoutSize
in interface LayoutManager2
target
- the Container that needs to be laid out
AWTError
- if checkContainerArgAndThread
objects to container
IllegalStateException
- if calling thread is not EventQueue
's dispatch thread
minimumLayoutSize
,
preferredLayoutSize
public void addLayoutComponent(String name, Component comp) throws IllegalStateException
addLayoutComponent
in interface LayoutManager
name
- the name of the Componentcomp
- the Component
IllegalStateException
- if calling thread is not EventQueue
's dispatch thread
public void addLayoutComponent(Component comp, Object constraints) throws IllegalStateException
addLayoutComponent
in interface LayoutManager2
comp
- the Componentconstraints
- constraints
IllegalStateException
- if calling thread is not EventQueue
's dispatch thread
public void invalidateLayout(Container target) throws AWTError, IllegalStateException
invalidateLayout
in interface LayoutManager2
target
- the affected container
AWTError
- if checkContainerArgAndThread
objects to container
IllegalStateException
- if calling thread is not EventQueue
's dispatch thread
public void layoutContainer(Container target) throws AWTError, IllegalStateException
layoutContainer
in interface LayoutManager
target
- the container to lay out
AWTError
- if checkContainerArgAndThread
objects to container
IllegalStateException
- if calling thread is not EventQueue
's dispatch thread
public void removeLayoutComponent(Component comp) throws IllegalStateException
removeLayoutComponent
in interface LayoutManager
comp
- the Component
IllegalStateException
- if calling thread is not EventQueue
's dispatch thread
protected void checkContainerArgAndThread(Container target) throws AWTError, IllegalStateException
AWTError
- if target is null or isn't the first Container used by this instance
IllegalStateException
- if calling thread is not EventQueue
's dispatch thread
protected void revalidateLayout(Container target)
protected void increaseSizeForInsets(Dimension size, Container target)
protected Dimension decreaseSizeForInsets(Dimension size, Insets insets)
protected int calcXOffset(Component c, Dimension allocSize, Insets insets)
protected int calcYOffset(Component c, Dimension allocSize, Insets insets)
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |