SlideShare a Scribd company logo
Understanding Layout Managers
By
Sana Mateen
Understanding Layout Managers
• All of the components that we have shown so far have been positioned by the
default layout manager.
• a layout manager automatically arranges your controls within a window by
using some type of algorithm
• Each Container object has a layout manager associated with it. A layout
manager is an instance of any class that implements the LayoutManager
interface.
• The layout manager is set by the setLayout( ) method. If no call to setLayout(
) is made, then the default layout manager is used.
• Whenever a container is resized (or sized for the first time), the layout
manager is used to position each of the components within it.
• The setLayout( ) method has the following general form:
• void setLayout(LayoutManager layoutObj)
• Here, layoutObj is a reference to the desired layout manager.
• If you wish to disable the layout manager and position components
manually, pass null for layoutObj.
• If you do this, you will need to determine the shape and position of each
component manually, using the setBounds( ) method defined by Component
Understanding Layout Managers
• Each layout manager keeps track of a list of components that are stored
by their names.
• The layout manager is notified each time you add a component to a
container.
• Whenever the container needs to be resized, the layout manager is
consulted via its minimumLayoutSize( ) and preferredLayoutSize( )
methods.
• Each component that is being managed by a layout manager contains the
getPreferredSize( ) and getMinimumSize( ) methods.
• These return the preferred and minimum size required to display each
component.
• The layout manager will honor these requests if at all possible, while
maintaining the integrity of the layout policy. You may override these
methods for controls that you subclass.
• Default values are provided otherwise.
FlowLayout
• FlowLayout is the default layout manager.
• This is the layout manager that the preceding examples have used.
FlowLayout implements a simple layout style, which is similar to how
words flow in a text editor.
• The direction of the layout is governed by the container’s component
orientation property, which, by default, is left to right, top to bottom.
• Therefore, by default, components are laid out line-by-line beginning at
the upper-left corner.
• In all cases, when a line is filled, layout advances to the next line.
• A small space is left between each component, above and below, as well
as left and right.
• Here are the constructors for FlowLayout:
• FlowLayout( )
• FlowLayout(int how)
• FlowLayout(int how, int horz, int vert)
FlowLayout
• The first form creates the default layout, which centers components and
leaves five pixels of space between each component.
• The second form lets you specify how each line is aligned.
• Valid values for how are as follows:
• FlowLayout.LEFT
• FlowLayout.CENTER
• FlowLayout.RIGHT
• FlowLayout.LEADING
• FlowLayout.TRAILING
• These values specify left, center, right, leading edge, and trailing edge
alignment, respectively.
• The third constructor allows you to specify the horizontal and vertical
space left between components in horz and vert, respectively
Understanding layout managers
Understanding layout managers
BorderLayout
• The BorderLayout class implements a common layout style for top-level
windows.
• It has four narrow, fixed-width components at the edges and one large
area in the center.
• The four sides are referred to as north, south, east, and west. The
middle area is called the center.
• Here are the constructors defined by BorderLayout:
• BorderLayout( )
• BorderLayout(int horz, int vert)
• The first form creates a default border layout.
• The second allows you to specify the horizontal and vertical space left
between components in horz and vert, respectively.
• BorderLayout defines the following constants that specify the regions:
• BorderLayout.CENTER,BorderLayout.SOUTH,BorderLayout.EAST
BorderLayout.WEST ,BorderLayout.NORTH
BorderLayout
• When adding components, you will use these constants with the following
form of add( ), which is defined by Container:
• void add(Component compRef, Object region)
• Here, compRef is a reference to the component to be added, and region
specifies where the component will be added.
Understanding layout managers
Understanding layout managers
Understanding layout managers
GridLayout
• GridLayout lays out components in a two-dimensional grid.
• When you instantiate a GridLayout, you define the number of rows and
columns.
• The constructors supported by GridLayout are shown here:
• GridLayout( )
• GridLayout(int numRows, int numColumns)
• GridLayout(int numRows, int numColumns, int horz, int vert)
• The first form creates a single-column grid layout.
• The second form creates a grid layout with the specified number of rows
and columns.
• The third form allows you to specify the horizontal and vertical space
left between components in horz and vert, respectively.
• Either numRows or numColumns can be zero.
• Specifying numRows as zero allows for unlimitedlength columns.
• Specifying numColumns as zero allows for unlimited-length rows
Understanding layout managers
Understanding layout managers
Understanding layout managers
Understanding layout managers
Understanding layout managers
Understanding layout managers
CardLayout
• The CardLayout class is unique among the other layout managers in that
it stores several different layouts.
• Each layout can be thought of as being on a separate index card in a deck
that can be shuffled so that any card is on top at a given time.
• This can be useful for user interfaces with optional components that can
be dynamically enabled and disabled upon user input.
• You can prepare the other layouts and have them hidden, ready to be
activated when needed.
• CardLayout provides these two constructors:
• CardLayout( )
• CardLayout(int horz, int vert)
• The first form creates a default card layout.
• The second form allows you to specify the horizontal and vertical space
left between components in horz and vert, respectively.
CardLayout
• Use of a card layout requires a bit more work than the other layouts.
• The cards are typically held in an object of type Panel.
• This panel must have CardLayout selected as its layout manager.
• The cards that form the deck are also typically objects of type Panel.
• Thus, you must create a panel that contains the deck and a panel for each card
in the deck.
• Next, you add to the appropriate panel the components that form each card.
• You then add these panels to the panel for which CardLayout is the layout
manager.
• Finally, you add this panel to the window. Once these steps are complete, you
must provide some way for the user to select between cards.
• One common approach is to include one push button for each card in the
deck.
• When card panels are added to a panel, they are usually given a name.
• Thus, most of the time, you will use this form of add( ) when adding cards to a
panel:
• void add(Component panelRef, Object name)
• Here, name is a string that specifies the name of the card whose panel is
CardLayout
• After you have created a deck, your program activates a card by calling
one of the following methods defined by CardLayout:
• void first(Container deck)
• void last(Container deck)
• void next(Container deck)
• void previous(Container deck)
• void show(Container deck, String cardName)
• Here, deck is a reference to the container (usually a panel) that holds the
cards, and cardName is the name of a card.
• Calling first( ) causes the first card in the deck to be shown.
• To show the last card, call last( ).
• To show the next card, call next( ).
• To show the previous card, call previous( ).
• Both next( ) and previous( ) automatically cycle back to the top or
bottom of the deck, respectively.
• The show( ) method displays the card whose name is passed in
cardName.
Understanding layout managers
Understanding layout managers
Understanding layout managers
GridBagLayout
• A good way to do this is to use a grid bag layout, which is specified by the
GridBagLayout class.
• What makes the grid bag useful is that you can specify the relative
placement of components by specifying their positions within cells
inside a grid.
• The key to the grid bag is that each component can be a different size,
and each row in the grid can have a different number of columns.
• This is why the layout is called a grid bag. It’s a collection of small grids
joined together.
• The location and size of each component in a grid bag are determined by
a set of constraints linked to it.
• The constraints are contained in an object of type GridBagConstraints.
• Constraints include the height and width of a cell, and the placement of a
component, its alignment, and its anchor point within the cell.
• The general procedure for using a grid bag is to first create a new GridBagLayout
object and to make it the current layout manager.
• Then, set the constraints that apply to each component that will be added to the
grid bag.
• Finally, add the components to the layout manager. Although GridBagLayout is a
bit more complicated than the other layout managers, it is still quite easy to use
once you understand how it works.
• GridBagLayout defines only one constructor, which is shown here:
• GridBagLayout( )
• GridBagLayout defines several methods, of which many are protected and not
for general use.
• There is one method, however, that you must use: setConstraints( ). It is shown
here:
• void setConstraints(Component comp, GridBagConstraints cons)
• Here, comp is the component for which the constraints specified by cons apply.
• This method sets the constraints that apply to each component in the grid bag.
• The key to successfully using GridBagLayout is the proper setting of the
constraints, which are stored in a GridBagConstraints object.
GridBagConstraints defines several fields that you can set to govern the size,
placement, and spacing of a component.
Understanding layout managers
Understanding layout managers
Understanding layout managers

More Related Content

PPT
PPT
Java layoutmanager
PPTX
PPTX
Xml dtd
PPT
Quiz app(j tabbed pane,jdialog,container,actionevent,jradiobutton,buttongroup...
PPTX
Http request and http response
PPTX
Xml schema
Java layoutmanager
Xml dtd
Quiz app(j tabbed pane,jdialog,container,actionevent,jradiobutton,buttongroup...
Http request and http response
Xml schema

Viewers also liked (9)

PPTX
Xml dom
PPTX
Dom parser
PPTX
Reading init param
PPTX
Jsp elements
PPTX
Jdbc in servlets
PPTX
Using cookies and sessions
PPT
Events1
PPTX
Intro xml
PPTX
Xml dom
Dom parser
Reading init param
Jsp elements
Jdbc in servlets
Using cookies and sessions
Events1
Intro xml
Ad

Similar to Understanding layout managers (20)

PPT
Layout managementand event handling
PPTX
3_ppt_Layout.pptxgßbdbdbdbsbsbsbbsbsbsbsbsb
PDF
8layout Managers
PPT
24-BuildingGUIs Complete Materials in Java.ppt
PPTX
LAYOUT.pptx
PDF
Module 2
PPTX
Advanced Java programming
PPT
Graphical User Interface in JAVA
PPTX
UNIT-I.pptx awt advance java abstract windowing toolkit and swing
PPT
Java AWR LayoutManagers for B. Tech. 2nd Year.ppt
PPTX
HASHIR_PPT about java coding which is frontend section.pptx
PPT
Chap1 1 4
PPT
Windows Programming with AWT
PPT
java swing
PPTX
Abstract Window Toolkit_Event Handling_python
PPTX
AWT.pptx
DOCX
Java gives us layout managers whose responsibility it is to determine.docx
PPTX
VsisbsvdisvdkdbdidvdkdbdifjdfbjffAWT.pptx
PDF
Unit-1 awt advanced java programming
PPTX
Chapter 11.3
Layout managementand event handling
3_ppt_Layout.pptxgßbdbdbdbsbsbsbbsbsbsbsbsb
8layout Managers
24-BuildingGUIs Complete Materials in Java.ppt
LAYOUT.pptx
Module 2
Advanced Java programming
Graphical User Interface in JAVA
UNIT-I.pptx awt advance java abstract windowing toolkit and swing
Java AWR LayoutManagers for B. Tech. 2nd Year.ppt
HASHIR_PPT about java coding which is frontend section.pptx
Chap1 1 4
Windows Programming with AWT
java swing
Abstract Window Toolkit_Event Handling_python
AWT.pptx
Java gives us layout managers whose responsibility it is to determine.docx
VsisbsvdisvdkdbdidvdkdbdifjdfbjffAWT.pptx
Unit-1 awt advanced java programming
Chapter 11.3
Ad

Recently uploaded (20)

DOCX
573137875-Attendance-Management-System-original
PPTX
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
PPTX
Welding lecture in detail for understanding
PDF
Operating System & Kernel Study Guide-1 - converted.pdf
PDF
Arduino robotics embedded978-1-4302-3184-4.pdf
PPTX
bas. eng. economics group 4 presentation 1.pptx
PDF
Structs to JSON How Go Powers REST APIs.pdf
PDF
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
PDF
PPT on Performance Review to get promotions
PPTX
Internet of Things (IOT) - A guide to understanding
PPTX
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
PDF
Model Code of Practice - Construction Work - 21102022 .pdf
PPTX
Strings in CPP - Strings in C++ are sequences of characters used to store and...
PPT
Project quality management in manufacturing
PPTX
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
PPTX
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
PPTX
UNIT 4 Total Quality Management .pptx
PPTX
web development for engineering and engineering
PPTX
additive manufacturing of ss316l using mig welding
PPTX
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
573137875-Attendance-Management-System-original
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
Welding lecture in detail for understanding
Operating System & Kernel Study Guide-1 - converted.pdf
Arduino robotics embedded978-1-4302-3184-4.pdf
bas. eng. economics group 4 presentation 1.pptx
Structs to JSON How Go Powers REST APIs.pdf
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
PPT on Performance Review to get promotions
Internet of Things (IOT) - A guide to understanding
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
Model Code of Practice - Construction Work - 21102022 .pdf
Strings in CPP - Strings in C++ are sequences of characters used to store and...
Project quality management in manufacturing
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
UNIT 4 Total Quality Management .pptx
web development for engineering and engineering
additive manufacturing of ss316l using mig welding
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx

Understanding layout managers

  • 2. Understanding Layout Managers • All of the components that we have shown so far have been positioned by the default layout manager. • a layout manager automatically arranges your controls within a window by using some type of algorithm • Each Container object has a layout manager associated with it. A layout manager is an instance of any class that implements the LayoutManager interface. • The layout manager is set by the setLayout( ) method. If no call to setLayout( ) is made, then the default layout manager is used. • Whenever a container is resized (or sized for the first time), the layout manager is used to position each of the components within it. • The setLayout( ) method has the following general form: • void setLayout(LayoutManager layoutObj) • Here, layoutObj is a reference to the desired layout manager. • If you wish to disable the layout manager and position components manually, pass null for layoutObj. • If you do this, you will need to determine the shape and position of each component manually, using the setBounds( ) method defined by Component
  • 3. Understanding Layout Managers • Each layout manager keeps track of a list of components that are stored by their names. • The layout manager is notified each time you add a component to a container. • Whenever the container needs to be resized, the layout manager is consulted via its minimumLayoutSize( ) and preferredLayoutSize( ) methods. • Each component that is being managed by a layout manager contains the getPreferredSize( ) and getMinimumSize( ) methods. • These return the preferred and minimum size required to display each component. • The layout manager will honor these requests if at all possible, while maintaining the integrity of the layout policy. You may override these methods for controls that you subclass. • Default values are provided otherwise.
  • 4. FlowLayout • FlowLayout is the default layout manager. • This is the layout manager that the preceding examples have used. FlowLayout implements a simple layout style, which is similar to how words flow in a text editor. • The direction of the layout is governed by the container’s component orientation property, which, by default, is left to right, top to bottom. • Therefore, by default, components are laid out line-by-line beginning at the upper-left corner. • In all cases, when a line is filled, layout advances to the next line. • A small space is left between each component, above and below, as well as left and right. • Here are the constructors for FlowLayout: • FlowLayout( ) • FlowLayout(int how) • FlowLayout(int how, int horz, int vert)
  • 5. FlowLayout • The first form creates the default layout, which centers components and leaves five pixels of space between each component. • The second form lets you specify how each line is aligned. • Valid values for how are as follows: • FlowLayout.LEFT • FlowLayout.CENTER • FlowLayout.RIGHT • FlowLayout.LEADING • FlowLayout.TRAILING • These values specify left, center, right, leading edge, and trailing edge alignment, respectively. • The third constructor allows you to specify the horizontal and vertical space left between components in horz and vert, respectively
  • 8. BorderLayout • The BorderLayout class implements a common layout style for top-level windows. • It has four narrow, fixed-width components at the edges and one large area in the center. • The four sides are referred to as north, south, east, and west. The middle area is called the center. • Here are the constructors defined by BorderLayout: • BorderLayout( ) • BorderLayout(int horz, int vert) • The first form creates a default border layout. • The second allows you to specify the horizontal and vertical space left between components in horz and vert, respectively. • BorderLayout defines the following constants that specify the regions: • BorderLayout.CENTER,BorderLayout.SOUTH,BorderLayout.EAST BorderLayout.WEST ,BorderLayout.NORTH
  • 9. BorderLayout • When adding components, you will use these constants with the following form of add( ), which is defined by Container: • void add(Component compRef, Object region) • Here, compRef is a reference to the component to be added, and region specifies where the component will be added.
  • 13. GridLayout • GridLayout lays out components in a two-dimensional grid. • When you instantiate a GridLayout, you define the number of rows and columns. • The constructors supported by GridLayout are shown here: • GridLayout( ) • GridLayout(int numRows, int numColumns) • GridLayout(int numRows, int numColumns, int horz, int vert) • The first form creates a single-column grid layout. • The second form creates a grid layout with the specified number of rows and columns. • The third form allows you to specify the horizontal and vertical space left between components in horz and vert, respectively. • Either numRows or numColumns can be zero. • Specifying numRows as zero allows for unlimitedlength columns. • Specifying numColumns as zero allows for unlimited-length rows
  • 20. CardLayout • The CardLayout class is unique among the other layout managers in that it stores several different layouts. • Each layout can be thought of as being on a separate index card in a deck that can be shuffled so that any card is on top at a given time. • This can be useful for user interfaces with optional components that can be dynamically enabled and disabled upon user input. • You can prepare the other layouts and have them hidden, ready to be activated when needed. • CardLayout provides these two constructors: • CardLayout( ) • CardLayout(int horz, int vert) • The first form creates a default card layout. • The second form allows you to specify the horizontal and vertical space left between components in horz and vert, respectively.
  • 21. CardLayout • Use of a card layout requires a bit more work than the other layouts. • The cards are typically held in an object of type Panel. • This panel must have CardLayout selected as its layout manager. • The cards that form the deck are also typically objects of type Panel. • Thus, you must create a panel that contains the deck and a panel for each card in the deck. • Next, you add to the appropriate panel the components that form each card. • You then add these panels to the panel for which CardLayout is the layout manager. • Finally, you add this panel to the window. Once these steps are complete, you must provide some way for the user to select between cards. • One common approach is to include one push button for each card in the deck. • When card panels are added to a panel, they are usually given a name. • Thus, most of the time, you will use this form of add( ) when adding cards to a panel: • void add(Component panelRef, Object name) • Here, name is a string that specifies the name of the card whose panel is
  • 22. CardLayout • After you have created a deck, your program activates a card by calling one of the following methods defined by CardLayout: • void first(Container deck) • void last(Container deck) • void next(Container deck) • void previous(Container deck) • void show(Container deck, String cardName) • Here, deck is a reference to the container (usually a panel) that holds the cards, and cardName is the name of a card. • Calling first( ) causes the first card in the deck to be shown. • To show the last card, call last( ). • To show the next card, call next( ). • To show the previous card, call previous( ). • Both next( ) and previous( ) automatically cycle back to the top or bottom of the deck, respectively. • The show( ) method displays the card whose name is passed in cardName.
  • 26. GridBagLayout • A good way to do this is to use a grid bag layout, which is specified by the GridBagLayout class. • What makes the grid bag useful is that you can specify the relative placement of components by specifying their positions within cells inside a grid. • The key to the grid bag is that each component can be a different size, and each row in the grid can have a different number of columns. • This is why the layout is called a grid bag. It’s a collection of small grids joined together. • The location and size of each component in a grid bag are determined by a set of constraints linked to it. • The constraints are contained in an object of type GridBagConstraints. • Constraints include the height and width of a cell, and the placement of a component, its alignment, and its anchor point within the cell.
  • 27. • The general procedure for using a grid bag is to first create a new GridBagLayout object and to make it the current layout manager. • Then, set the constraints that apply to each component that will be added to the grid bag. • Finally, add the components to the layout manager. Although GridBagLayout is a bit more complicated than the other layout managers, it is still quite easy to use once you understand how it works. • GridBagLayout defines only one constructor, which is shown here: • GridBagLayout( ) • GridBagLayout defines several methods, of which many are protected and not for general use. • There is one method, however, that you must use: setConstraints( ). It is shown here: • void setConstraints(Component comp, GridBagConstraints cons) • Here, comp is the component for which the constraints specified by cons apply. • This method sets the constraints that apply to each component in the grid bag. • The key to successfully using GridBagLayout is the proper setting of the constraints, which are stored in a GridBagConstraints object. GridBagConstraints defines several fields that you can set to govern the size, placement, and spacing of a component.