31
niversity of Limerick Software Architecture Java Layout Managers

University of Limerick1 Software Architecture Java Layout Managers

Embed Size (px)

Citation preview

Page 1: University of Limerick1 Software Architecture Java Layout Managers

University of Limerick 1

Software Architecture

Java Layout Managers

Page 2: University of Limerick1 Software Architecture Java Layout Managers

University of Limerick 2

Lecture Objectives Understanding the difference between

Containers and Components Understand what a Layout Managers is

Page 3: University of Limerick1 Software Architecture Java Layout Managers

Frames and components

menus

labels

windows (frames)

checkboxes

radio buttons

borders

buttons

Page 4: University of Limerick1 Software Architecture Java Layout Managers

Containers and Components Most of the GUI classes we deal with can be

classified as either– Containers : Objects capable of containing other

graphical objects– Components : Stand alone graphical widgets with

no containment ability These classes inherit from (J)Component or

Container respectively

Page 5: University of Limerick1 Software Architecture Java Layout Managers

Containers Containers are Swing components that hold

other components Containers can be nested (containers in

containers) Containers use a LayoutManager to

determine how to arrange the components A Swing frame hold a container in it’s content

pane

Page 6: University of Limerick1 Software Architecture Java Layout Managers

University of Limerick 6

Example Containers

JPanel– Panel is the simplest container class. A panel provides space

in which an application can attach any other component. The default layout manager for a Panel is FlowLayout.

Window– A Window object is a top-level window with no borders and

no menubar. JFrame

– A Frame is a top-level window with title and border; default layout is BorderLayout.

JDialog– A window that takes input from the user.

Page 7: University of Limerick1 Software Architecture Java Layout Managers

Swing: frame structure

JFrame title bar

content pane

Page 8: University of Limerick1 Software Architecture Java Layout Managers

More on Containers (1) Swing provides four useful top-level container

classes: JFrame, JApplet, JDialog & JWindow. JPanel’s are intermediate containers. They are

used to store other GUI objects, such as buttons, labels, textfields, etc.

Note there are other intermediate containers, such as TabbedPane, Toolbar, etc.

Page 9: University of Limerick1 Software Architecture Java Layout Managers

Components and Containers GUI Objects such as labels, buttons, textfields,

radiobuttons are consider atomic components…they DO NOT store other GUI objects within themselves.

Every top-level container contains an intermediate container, known as a content pane.

Page 10: University of Limerick1 Software Architecture Java Layout Managers

University of Limerick 10

Components Standard GUI controls like buttons, menus,

scrollbars, text fields and lists are typical components

Swing also supplies some complex controls like Color, File and Font choosers, spreadsheet-like tables and Windows Explorer-like hierarchical trees

Page 11: University of Limerick1 Software Architecture Java Layout Managers

More on Components So whenever you add an atomic component

or intermediate container to a top-level container (e.g. JFrame), add it to the content pane.

E.g. Incorrect– add(new JButton(“hello”));

Correct– getContentPane().add(new JButton(“hello”));

Page 12: University of Limerick1 Software Architecture Java Layout Managers

University of Limerick 12

Adding Components to Containers

Containers exist to hold components which create the UI experience for the user

If you don't care about the layout of the components in the container, adding them is simple

Container contentPane = getContentPane();JButton aButton = new JButton(“Start”);contentPane.add(aButton);

Page 13: University of Limerick1 Software Architecture Java Layout Managers

University of Limerick 13

Layout managers

Using a layout manager you can choose one of a small number of standard arrangments for subwindows

You create a “layout manager” object - an instance of a class that– keeps track of subwindows and their (minimum) sizes– chooses how to position them in the enclosing window

you associate this layout manager with the window

Page 14: University of Limerick1 Software Architecture Java Layout Managers

Arranging components

containers

components

Page 15: University of Limerick1 Software Architecture Java Layout Managers

ExampleJPanel bluePanel = new JPanel();bluePanel.setBackground(Color.blue);bluePanel.setOpaque(true);bluePanel.setPreferredSize(new Dimension(200, 200));

….JPanel normalPanel = new JPanel(new GridLayout(0, 2));JLabel blackLabel = new JLabel("Black Label");

blackLabel.setOpaque(true);blackLabel.setBackground(Color.black);blackLabel.setForeground(Color.white);

JLabel greenLabel = new JLabel("Green Label");greenLabel.setOpaque(true);greenLabel.setBackground(Color.green);

normalPanel.add(blackLabel);normalPanel.add(greenLabel);

Page 16: University of Limerick1 Software Architecture Java Layout Managers

Why Layout Managers So the programmer no longer bares the

burden of specifying the exact position and dimension of each component

Page 17: University of Limerick1 Software Architecture Java Layout Managers

Layout Managers each container manages layout of its

components the programmer just adds components, the

container looks after layout the container uses a Layout Manager to

handle the layout different layout managers are available layout can be specified by specifying layout

managers for containers

Page 18: University of Limerick1 Software Architecture Java Layout Managers

Layout Managers Layout managers

– FlowLayout– BorderLayout– GridLayout– GridBagLayout– CardLayout

Page 19: University of Limerick1 Software Architecture Java Layout Managers

FlowLayout

Arranges all components in a horizontal row Components flow into next line when no space in row The FlowLayout managers always honours a

components preferred size Default layout for JPanels

Page 20: University of Limerick1 Software Architecture Java Layout Managers

Flow Layout Managers getContentPane().setLayout(new FlowLayout(10, 30,

FlowLayout.LEFT));JButton JButNorth = new JButton("AAAA");JButton JButCenter = new JButton("BBB");JButton JButWest = new JButton("CCC");JButton JButEast = new JButton("DDDD");JButton JButSouth = new JButton("EEEE");

getContentPane().add(JButNorth);getContentPane().add(JButWest);getContentPane().add(JButSouth);getContentPane().add(JButEast);getContentPane().add(JButCenter);

Page 21: University of Limerick1 Software Architecture Java Layout Managers

BorderLayout

Content panes (main containers in JFrame, JDialog, JApplet) use BorderLayout by default)

Holds up to five components in fixed places

center

west

east

south

north

Page 22: University of Limerick1 Software Architecture Java Layout Managers

BorderLayout

The BorderLayout manager honours the preferred height of the North and South sectors but expands the width to fill the entire container

Honours the preferred width of the East and West sectors but expands the height to fill the entire container

Page 23: University of Limerick1 Software Architecture Java Layout Managers

Border Layout Managers ...

JButton JButNorth = new JButton("North");JButton JButCenter = new JButton("Center");JButton JButWest = new JButton("West");JButton JButEast = new JButton("East");JButton JButSouth = new JButton("South");

getContentPane().add(JButNorth, BorderLayout.NORTH);getContentPane().add(JButWest, BorderLayout.WEST);getContentPane().add(JButSouth, BorderLayout.SOUTH);getContentPane().add(JButEast, BorderLayout.EAST);getContentPane().add(JButCenter);

...

Page 24: University of Limerick1 Software Architecture Java Layout Managers

GridLayout

Arranges components in a grid The GridLayout managers ignores a components preferred

size– Components are expanded to fill the sector

Page 25: University of Limerick1 Software Architecture Java Layout Managers

Grid Layout Managers getContentPane().setLayout(new GridLayout(3, 3));

JButton JButNorth = new JButton("Click");JButton JButCenter = new JButton("Exit");JButton JButWest = new JButton("Save");JButton JButEast = new JButton("Bye");JButton JButSouth = new JButton("Hello");

getContentPane().add(JButNorth);getContentPane().add(JButWest);getContentPane().add(JButSouth);getContentPane().add(JButEast);getContentPane().add(JButCenter);

Page 26: University of Limerick1 Software Architecture Java Layout Managers

CardLayout

Arranges components in time rather than space Similar to a tabbed panel without the tabs Components are arranged in sequence and only one is

displayed at a time– N.B. A component may be a JPanel

Page 27: University of Limerick1 Software Architecture Java Layout Managers

GridBag Layout

Most powerful layout manager Divides layout into an array of cell but cell may have

different height and widths Considered the most difficult to use

Page 28: University of Limerick1 Software Architecture Java Layout Managers

Layout Managers By default every container has a layout

manager JPanel use FlowLayout by default Content panes (main containers in JFrame,

JApplet) use BorderLayout by default) To override the default layout for any

containers use the setLayout method or specify it during constructor

Remember for top level containers use getContentPane().setLayout(...).

Page 29: University of Limerick1 Software Architecture Java Layout Managers

Combining Layout managers Most moderately sophisticated UI designs are

too complex for the layout requirements to be satisfied by a single layout manager

So you will often need to combine two or more separate layout managers to achieve the desired affect overall

Page 30: University of Limerick1 Software Architecture Java Layout Managers

Combining Layout managers This is accomplished by using extra container

objects (JPanels) with different layout managers within a main container like a JFrame

It is also possible to specify that you do not want to employ a layout manager, and position subwindows yourself

Page 31: University of Limerick1 Software Architecture Java Layout Managers

University of Limerick 31

Calculator Example public CalculatorPanel() { setLayout(new BorderLayout()); display = new JTextField("0"); display.setEditable(false); add(display, "North"); JPanel p = new JPanel(); p.setLayout(new GridLayout(4, 4)); String buttons = "789/456*123-0.=+"; for (int i = 0; i < buttons.length(); i++) addButton(p, buttons.substring(i, i + 1)); add(p, "Center");}

New panel

Add new panel toexisting panel

Set layout forpanel