0% found this document useful (0 votes)
10 views24 pages

Java Layout Managers Explained

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views24 pages

Java Layout Managers Explained

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd

Layout Managers

• Layout manager- An interface class that is part


of the JDK
– Aligns your components so they neither crowd
each other nor overlap
– Can assign layout managers within layout
managers
– For example:
• One layout manager arranges components in equally
spaced columns and rows
Defaults
• Panel = FlowLayout
• Applet = FlowLayout
• Frame = BorderLayout
Layout Managers
BorderLayout Managers
• BorderLayout manager- The default manager for all
content panes
– Can use the BorderLayout class with any container that
has five or fewer components
– Components fill the screen with five regions named North,
South, East, West, and Center
– The compiler determines the exact size of each
component based on the components contents
– When BorderLayout is used with less than five
components, any empty regions disappear
BorderLayout

• Five areas
– NORTH, SOUTH, EAST, WEST and CENTER
– Not all areas must be used
– Do not assume a default area for components
– Centre gets as much area as possible

• Specify location as argument of add method


– [Link](new BorderLayout());
– [Link](new JButton(“Button 1 (NORTH)”),
[Link]);

• Setting gaps between components (default = 0)


– [Link](int gap);
– [Link](int gap);
– BorderLayout(int horizontalGap, int verticalGap) - Constructor
5
Positions using the BorderLayout
FlowLayout Manager
• Default type for Panels and Applets.
• A lot like a word processor:
– Arranges components in horizontal rows.
– Components “wrap” to the next line if they don’t
fit across.
• Each component retains its default size
• Specify in constructor:
– [Link], [Link],
[Link]
Flow Layout
- To arrange Components in rows across the width of a container
- Each component that you add is placed to the right of previously
added components
width=400 height=50

width=100 height=120

8 CS 3331
FlowLayout

• Very simple - JPanel’s default


• Components in row(s)
• At preferred size
• Alignment
– [Link]
– [Link]
– [Link]

• Gaps
– Default = 5
– Specifying - setter hGap vGap methods or via constructor

9
GridLayout Managers
• GridLayout- To arrange Components into
equal rows and columns
• GridLayout( rows, columns )
– You indicate the numbers of rows and columns
you want, and then the container surface is
divided into a grid
– Specify rows first, columns second
– As you add components they are positioned left-
to-right across each row in sequence
Grid Layout

3x2 grid

0x1 grid

1x0 grid

11
GridLayout

• Grid of cells - all same size


• Components take all space in a cell
• Gaps
– default = 5
– use setter methods hGap and vGap
– or via arguments to constructor
• Re-sizing
– Cells resize to be as large as possible in given
window / container

12
GridBagLayout

• Very flexible (and complex!)


• Rows can have different heights
• Columns can have different lengths
• Uses cells in a grid
GridBagLayout gridbag = new GridBagLayout();
GridBagConstraints c = new GridBagConstraints();

JPanel pane = new JPanel();


[Link](gridbag);

//--- For each component to be added to this container:


//--- ...Create the component...
//--- ...Set instance variables in the GridBagConstraints instance...
[Link](theComponent, c);
[Link](theComponent);

13
GridBagLayout
• Constraints
– set in an instance of a gridBagConstraints Object
– gridx and gridy - The row and column of the upper left of the
component
– Anchor - Where to display within cell when component is smaller than it
– fill - How to size component when cell is larger than components
requested size
– insets - External padding - min space between component and cell edges
– ipadx, ipady - Internal padding - What to add to min size of components
– weightx and weighty - How to distribute extra space (padding)
– gridwidth and gridheight - Number of columns or rows the component
uses

14
• GridBagLayout- Allows you to add
components to precise locations within the
grid, as well as to indicate that specific
Components should span multiple rows or
columns within the grid
• This class is difficult to use because you must
set the position and size for each component
Card Layout Managers
• CardLayout- Generates a stack of containers or
components, one on top of another
• Each container in the group is referred to as a card
• Only one component is visible at a time
• A card layout is created from the CardLayout class
using one of two constructors
– CardLayout() creates a new card layout without a
horizontal or vertical gap
– CardLayout(int hgap, int vgap) creates a new card layout
with the specified horizontal and vertical gaps
BoxLayout

• Components on top of/ next to each other


– Direction is your choice

• Tries to size components at preferred height for


Y_AXIS or width for X_AXIS

• Width as largest component width


– See above picture
17
BoxLayout
• Space fillers
– Rigid - fixed-size space between two components

– Glue - taking up no space unless you pull apart the components that it's sticking
to. Helps reposition extra space (default is at end)

– Custom - Use this to specify a component with whatever minimum, preferred,


and maximum sizes you want

18
BoxLayout

• Component sizes
– Respects Max, Min and Preferred Sizes of components

• Alignment
– Comes into play when not all components are the same width
– Can specify Left (0), Centre (0.5) or Right (1). Or Top Middle Bottom

• If you are having layout problems, first treat as an


Alignment issue, then examine sizes.

19
• BoxLayout- Components are arranged in
either a single row or a single column
• The box layout manager tries to make all the
components the same height(row) or
width(column) so components do not spill
over
BoxLayout
• Requires two arguments
– The first refers to the container to which the
layout manager applies
– The second is a constant
• BoxLayout.X_Axis for a row arrangement
• BoxLayout.Y_Axis for a column arrangement
Using JPanels
• JPanels- Increase the number of possible component
arrangements by using the JPanel class
• A JPanel is similar to a JWindow in that a JPanel is a
surface on which you can place components
• JPanel is a Container so it can contain other
components
• By using JPanels within JPanels, you can create an
infinite variety of screen layouts
JPanel
Frame
Panel

Panel Panel Panel

Label Label Label

Text Field Text Field Text Field

Scrollbar Scrollbar Scrollbar

Panel

Button Button Button


Using JPanels
• JPanel object constructors
– JPanel() creates a new JPanel with a double buffer
and a flow layout
– JPanel(LayoutManager layout) creates a new
buffered JPanel with the specified layout manager

You might also like