LayoutManagers The LayoutManagers are used to arrange components in a particular manner. LayoutManager is an interface that is implemented by all the classes.

Slides:



Advertisements
Similar presentations
G5BUID - Java Swing Laying out components Manage realized components Determine size and position Each container has a layout manager (usually)
Advertisements

Unit 121 A layout manager is an object that determines the manner in which components are arranged in a container. Each layout manager implements one of.
GridLayout, n*m grid. Column width/row height adjusted to fith the widest/highest Component.
Graphical User Interfaces
TCU CoSc Programming with Java Visual Design (Chapter 5)
AWT Components. 2 Using AWT Components 4 Component –Canvas –Scrollbar –Button –Checkbox –Label –List –Choice –TextComponent –TextArea –TextField 4 Component.
Graphic User Interfaces Layout Managers Event Handling.
CMSC 341 Building Java GUIs. 09/26/2007 CMSC 341 GUI 2 Why Java GUI Development? Course is about Data Structures, not GUIs. We are giving you the opportunity.
Advanced Swing. Advanced Layout Managers GridBagLayout – Example: Web Browser (Grid Bag Layout)Web Browser (Grid Bag Layout) BoxLayout – Example: Web.
Abstract Windowing Toolkit Design Goal: –allow the programmer to build o GUI that looks good on all platforms –provide a well-designed object-oriented.
LayoutManager, J 1 Layout Manager. LayoutManager, J 2 Layout Manager To each UI container there is a layout manager (an object). When you add a component.
GUI programming AWT(Abstract Windows Toolkit)-GUI components library when Java was introduced AWT was replaced by Swing components import javax.swing.*;
Understanding SWING Architecture CS 4170 UI Design Hrvoje Benko Oct. 9, 2001.
Unit 131 GUI Layout Managers Learning Outcomes oList and distinguish between the four most common, standard layout managers in Java. oUse these and other.
LAB SESSION 10 *LAYOUT MANAGER *LISTENERS. Laying the components manually by using a null layout is tedious. Each container object has a layout manager.
IEEM 110 Computing in Industrial Applications Basic User Interface in Java.
1 L48 Advanced GUI Component (3). 2 OBJECTIVES  To use additional layout managers.
Jan Containers Yangjun Chen Dept. Business Computing University of Winnipeg.
GUI Layout Managers Arkadiusz Edward Komenda. Outline Components and Containers Layout Managers Flow Layout Grid Layout Border Layout Nested Containers.
Java Swing Chris North cs3724: HCI. AWT to Swing AWT: Abstract Windowing Toolkit import java.awt.* Swing: new with Java2 import javax.swing.* Extends.
OOP (Java): Layout/ OOP Objectives – –describe the basic layout managers for GUIs Semester 2, GUI Layout.
MSc Workshop - © S. Kamin, U.Reddy Lect 5 – GUI Prog - 1 Lecture 5 – GUI Programming r Inner classes  this and super r Layout r Reading: m.
Rina System development with Java Instructors: Rina Zviel-Girshin Lecture 12.
Java Programming: Advanced Topics 1 Common Elements of Graphical User Interfaces Chapter 6.
Chapter 8: Getting Started with Graphics Programming Graphics Class Hierarchy Graphics Class Hierarchy Frames Frames The relationship between frame and.
1 Java Swing Layout Management. 2 Laying out components Manage realized components Manage realized components Determine size and position Determine size.
CompSci 100E 35.1 Graphical User Interfaces: GUIs  Components  Flat Layouts  Hierarchical Layouts  Designing a GUI  Coding a GUI.
Layout Managers Arranges and lays out the GUI components on a container.
© Marty Hall, Larry Brown, Web core programming 1 Layout Managers Arranging Elements in Windows.
GUI Basics. What is GUI? A graphical user interface (GUI) is a type of user interface item that allows people to interact with programs in more ways than.
1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.
Layout Managers CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
University of Limerick1 Software Architecture Java Layout Managers.
Object-Oriented Software Engineering
3461 Laying Out Components Interior Design for GUIs.
Copyright Curt Hill GridBag Layout Manager A flexible but complicated layout.
Grid Bag Layout Most Complex and Powerful Components can vary in size and can be added in any order Draw out on piece of paper first Columns and Rows:
Computer Science 209 GUIs Model/View/Controller Layouts.
CPSC 233 Tutorial Xin Apr 6, Reading files An example available on my website pages.cpsc.ucalgary.ca/~liuxin.
J McQuillanSE204:2004/2005: Lecture 3Slide 1 Specialised Components Can create specialised components. Do this by subclassing the component that you are.
AWT Layout Managers (Chapter 10) Java Certification Study Group January 21, 1999 Mark Roth.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 3 Designing the Interface with Layout Managers.
1 Layout Managers Layout managers –Provided for arranging GUI components –Provide basic layout capabilities –Processes layout details –Programmer can concentrate.
CSI 3125, Preliminaries, page 1 Layout Managers. CSI 3125, Preliminaries, page 2 Layout Managers The LayoutManagers are used to arrange components in.
Graphical User Interface (GUI) Two-Dimensional Graphical Shapes.
Java Swing - Lecture 3 Layout Management
Graphical User Interface (GUI)
Event Handler Methods Text field Object Responder JAVA AWT Environment: Messages are sent between JAVA Objects Screen Event Notification Press Button.
CSC 205 Programming II Lecture 7 AWT – Event Handling & Layout.
Session 10. Java Simplified / Session 10 / 2 of 36  An Applet is a Java program that can be executed with the help of a Java enabled browser.  Every.
Getting Started with GUI Programming Chapter 10 CSCI 1302.
Dept. of CSIE, National University of Tainan 10/21/2012 Arranging Components on a User Interface.
GUI.1 Graphical User Interfaces GUIs. GUI.2 The Plan Components Flat Layouts Hierarchical Layouts Designing a GUI Coding a GUI.
Unit 131 GUI Layout Managers Learning Outcomes oList and distinguish between the four most common, standard layout managers in Java. oUse these and other.
Chapter 7 A First Look at GUI Applications Layout Managers.
Object-Oriented Software Engineering JFrames with Swing.
Graphical User Interfaces -- GUIs
Swing JComponents.
GUIs Model/View/Controller Layouts
Modern Programming Language Java
University of Central Florida COP 3330 Object Oriented Programming
Core Java (Part-2) Jun 24, 2014 By : Ghanshyam Dhomse.
Tim McKenna Layout Mangers in Java Tim McKenna
AWT Components.
Chap 7. Building Java Graphical User Interfaces
Containers and Components
Tim McKenna Layout Mangers in Java Tim McKenna
Panels & Layout Managers
Layout Organization and Management
Presentation transcript:

LayoutManagers The LayoutManagers are used to arrange components in a particular manner. LayoutManager is an interface that is implemented by all the classes of layout managers. There are following classes that represents the layout managers: java.awt.BorderLayout java.awt.FlowLayout java.awt.GridLayout java.awt.CardLayout java.awt.GridBagLayout javax.swing.BoxLayout javax.swing.GroupLayout javax.swing.ScrollPaneLayout javax.swing.SpringLayout etc. Defined in the AWT Defined in Swing

Layout of Components BorderLayout FlowLayout CardLayout GridLayout north, south, west, east & center FlowLayout left to right & top down CardLayout stack of panels GridLayout tabular form (rows & columns) GridBagLayout tabular form(variable row heights and column widths)

Use Layout Managers Default layout managers Windows (Frames & Dialogs) setLayout(new BorderLayout()); setLayout(new CardLayout(()); setLayout(new FlowLayout()); setLayout(new GridLayout(rows,columns,xgap,ygap)); Default layout managers Windows (Frames & Dialogs) BorderLayout Panels (Applets) FlowLayout

BorderLayout The BorderLayout is used to arrange the components in five regions: north, south, east, west and center. Each region (area) may contain one component only. It is the default layout of frame or window. Five constants for each region: public static final int NORTH public static final int SOUTH public static final int EAST public static final int WEST public static final int CENTER

Constructors of BorderLayout class BorderLayout() creates a border layout but with no gaps between the components. JBorderLayout(int hgap, int vgap) creates a border layout with the given horizontal and vertical gaps between the components.

How to Use BorderLayout? import java.awt.*; public class TestBorderLayout { public static void main(String[] args){ Frame f = new Frame("TestBorderLayout"); f.setSize(200,200); f.add("North", new Button("North")); f.add("South", new Button("South")); f.add("East", new Button("East")); f.add("West", new Button("West")); f.add("Center", new Button("Center")); f.setVisible(true); }

Example of BorderLayout class import java.awt.*; import javax.swing.*; public class Border { JFrame f; Border(){ f=new JFrame(); JButton b1=new JButton("NORTH"); JButton b2=new JButton("SOUTH"); JButton b3=new JButton("EAST"); JButton b4=new JButton("WEST"); JButton b5=new JButton("CENTER");    f.add(b1,BorderLayout.NORTH);       f.add(b2,BorderLayout.SOUTH);       f.add(b3,BorderLayout.EAST);       f.add(b4,BorderLayout.WEST);       f.add(b5,BorderLayout.CENTER);              f.setSize(300,300);       f.setVisible(true);   }   public static void main(String[] args) {       new Border();  

GridLayout The GridLayout is used to arrange the components in rectangular grid. One component is displayed in each rectangle.

Constructors of GridLayout class GridLayout() creates a grid layout with one column per component in a row. GridLayout(int rows, int columns) creates a grid layout with the given rows and columns but no gaps between the components. GridLayout(int rows, int columns, int hgap, int vgap) creates a grid layout with the given rows and columns alongwith given horizontal and vertical gaps.

How to Use GridLayout? import java.awt.*; public class TestGridLayout { public static void main(String[] args){ Frame f = new Frame("TestGridLayout"); f.setSize(200,200); f.setLayout(new GridLayout(2,3)); f.add(new Button("Button 1")); f.add(new Button("Button 2")); f.add(new Button("Button 3")); f.add(new Button("Button 4")); f.add(new Button("Button 5")); f.setVisible(true); }

Example of GridLayout class import java.awt.*; import javax.swing.*; public class MyGridLayout{ JFrame f; MyGridLayout(){ f=new JFrame(); JButton b1=new JButton("1"); JButton b2=new JButton("2"); JButton b3=new JButton("3"); JButton b4=new JButton("4"); JButton b5=new JButton("5"); JButton b6=new JButton("6"); JButton b7=new JButton("7"); JButton b8=new JButton("8"); JButton b9=new JButton("9"); f.add(b1); f.add(b2); f.add(b3); f.add(b4); f.add(b5); f.add(b6); f.add(b7); f.add(b8); f.add(b9);          f.setLayout(new GridLayout(3,3));       f.setSize(300,300);       f.setVisible(true);   }   public static void main(String[] args) {       new MyGridLayout();  

FlowLayout The FlowLayout is used to arrange the components in a line, one after another (in a flow). It is the default layout of applet or panel. Fields of FlowLayout class: public static final int LEFT public static final int RIGHT public static final int CENTER public static final int LEADING public static final int TRAILING

Constructors of FlowLayout class FlowLayout() creates a flow layout with centered alignment and a default 5 unit horizontal and vertical gap. FlowLayout(int align) creates a flow layout with the given alignment and a default 5 unit horizontal and vertical gap. FlowLayout(int align, int hgap, int vgap) creates a flow layout with the given alignment and the given horizontal and vertical gap.

How to Use FlowLayout? import java.awt.*; public class TestFlowLayout { public static void main(String[] args){ Frame f = new Frame("TestFlowLayout"); f.setSize(200,200); f.setLayout(new FlowLayout()); f.add(new Button("Button 1")); f.add(new Button("Button 2")); f.add(new Button("Button 3")); f.add(new Button("Button 4")); f.add(new Button("Button 5")); f.setVisible(true); }

Example of FlowLayout class import java.awt.*; import javax.swing.*; public class MyFlowLayout{ JFrame f; MyFlowLayout(){ f=new JFrame(); JButton b1=new JButton("1"); JButton b2=new JButton("2"); JButton b3=new JButton("3"); JButton b4=new JButton("4"); JButton b5=new JButton("5"); f.add(b1); f .add(b2);   f.add(b3); f.add(b4); f.add(b5);              f.setLayout(new FlowLayout(FlowLayout.RIGHT));       //setting flow layout of right alignment          f.setSize(300,300);       f.setVisible(true);   }   public static void main(String[] args) {       new MyFlowLayout();  

Constructors of CardLayout class The CardLayout class manages the components in such a manner that only one component is visible at a time. It treats each component as a card that is why it is known as CardLayout. Constructors of CardLayout class CardLayout()  creates a card layout with zero horizontal and vertical gap. CardLayout(int hgap, int vgap)  creates a card layout with the given horizontal and vertical gap.

Commonly used methods CardLayout public void next(Container parent) It is used to flip to the next card of the given container. public void previous(Container parent) It is used to flip to the previous card of the given container. public void first(Container parent)  It is used to flip to the first card of the given container. public void last(Container parent)  It is used to flip to the last card of the given container. public void show(Container parent, String name) It is used to flip to the specified card with the given name.

How to Use CardLayout? import java.awt.*; public class TestCardLayout { public static void main(String[] args){ Frame f = new Frame("TestCard Layout"); f.setSize(200,200); f.setLayout(new CardLayout()); f.add("GraphicsPanel",new GraphicsPanel()); f.add("LabelPanel",new LabelPanel()); f.add("ButtonPanel",new ButtonPanel()); f.setVisible(true); }

Example of CardLayout class import java.awt.*; import java.awt.event.*; import javax.swing.*; public class CardLayoutExample extends JFrame implements ActionListener{ CardLayout card; JButton b1,b2,b3; Container c; CardLayoutExample(){ c=getContentPane(); card=new CardLayout(40,30); c.setLayout(card); b1=new JButton("Apple"); b2=new JButton("Boy"); b3=new JButton("Cat"); b1.addActionListener(this); b2.addActionListener(this); b3.addActionListener(this);     c.add("a",b1); c.add("b",b2); c.add("c",b3);   }    public void actionPerformed(ActionEvent e) {       card.next(c);    }      public static void main(String[] args)  CardLayoutExample cl=new CardLayoutExample();           cl.setSize(400,400);           cl.setVisible(true);           cl.setDefaultCloseOperation(EXIT_ON_CLOSE);       }  

GridBagLayout class The GridBagLayout class is a flexible layout manager that aligns components vertically and horizontally, without requiring that the components be of the same size. Each GridBagLayout object maintains a dynamic rectangular grid of cells, with each component occupying one or more cells, called its display area.

Constructor of GridBagLayout class GridBagLayout() Creates a grid bag layout manager.

setContraints(Component, GridBagContraints) Some of the information that the GridBagLayout needs to know about an object are: row and column number of cells spanned placement within its space stretch and shrink values This information is stored in an object of type GridBagContstraints and is associated with a component using setContraints(Component, GridBagContraints) This causes the layout manager to make a copy of the constraints and associate them with the object. Therefore you only need one of these GridBagContraints objects.

GridBagConstraints The following is a complete list of all of the constraints: anchor determines position in the display area fill determines if a component is stretched to fill the area gridheight and gridwidth determine the number of rows and columns in the component's area gridx and gridy determine the position of the component's area. insets determine a border around a component's area. ipadx and ipady allows the minimum or preferred size of a component to be adjusted. weightx and weighty determine the sizes of the rows and columns of the grids.

Example of GridBagLayout class import java.awt.*; import java.awt.event.*; import java.applet.*; public class GB01 extends Applet implements ActionListener { Button B1 = new Button("Button 1"); Button B2 = new Button("Button 2"); GridBagLayout gridbag; public void init() { setBackground(Color.yellow); gridbag = new GridBagLayout(); GridBagConstraints c = new GridBagConstraints(); setLayout(gridbag); c.weightx = 1; c.weighty = 1; c.gridx = 0; c.gridy = 0; c.anchor = GridBagConstraints.SOUTHWEST; gridbag.setConstraints(B1,c); add(B1); c.weightx = 0; c.gridx = 1; c.anchor = GridBagConstraints.NORTH; c.fill = GridBagConstraints.BOTH; gridbag.setConstraints(B2,c); add(B2); B1.addActionListener(this); B2.addActionListener(this); } public void actionPerformed(ActionEvent e) { repaint(); /*<applet code=GB01.class width=200 height=200> </applet>*/

Layout Manager Heuristics null FlowLayout GridLayout none, programmer sets x,y,w,h Left to right, Top to bottom BorderLayout CardLayout GridBagLayout n c One at a time w e JButton s