University of Limerick1 Software Architecture Java Layout Managers.

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

Introduction to Java 2 Programming
Introduction to Swing Components Chapter 14. Part of the Java Foundation Classes (JFC) Provides a rich set of GUI components Used to create a Java program.
Java Software Development Paradigm Lecture # 12. Basics of GUI.
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.
Corresponds with Chapter 12
Java Swing Recitation – 11/(20,21)/2008 CS 180 Department of Computer Science, Purdue University.
Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
Slides prepared by Rose Williams, Binghamton University Chapter 17 Swing I.
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.
CS102--Object Oriented Programming Lecture 19: – The Swing Package (II) Copyright © 2008 Xiaoyan Li.
GUI’s and eventhandlers in java Martin Jagersand.
1 GUI Elements in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
1 CMSC 132: Object-Oriented Programming II Nelson Padua-Perez William Pugh Department of Computer Science University of Maryland, College Park.
GUI Layout Managers Arkadiusz Edward Komenda. Outline Components and Containers Layout Managers Flow Layout Grid Layout Border Layout Nested Containers.
Unit 11 Object-oriented programming: Graphical user interface Jin Sa.
Chapter 8: Graphical User Interfaces Objectives - by the end of this chapter, you should be able to do the following: –write a simple graphical user interface.
Java Programming Chapter 10 Graphical User Interfaces.
OOP (Java): Layout/ OOP Objectives – –describe the basic layout managers for GUIs Semester 2, GUI Layout.
Applets and Frames CS 21a: Introduction to Computing I First Semester,
CSE 219 Computer Science III Graphical User Interface.
Java GUI building with Swing. 2 AWT (Abstract Window Toolkit) Present in all Java implementations Described in (almost) every Java textbook Adequate for.
Java Programming: Advanced Topics 1 Common Elements of Graphical User Interfaces Chapter 6.
10/24/20151 Java GUI Programming. 10/24/20152 What is a GUI? Java has standard packages for creating custom Graphical User Interfaces Some of the fundamental.
JFrame and JPanel CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Graphics and Event-Driven Programming in Java John C. Ramirez Department of Computer Science University of Pittsburgh.
Laying Out Components Interior Design for GUIs. Nov 042 What is Widget Layout? Positioning widgets in their container (typically a JPanel or a JFrame’s.
MSc Workshop - © S. Kamin, U. ReddyLect 3 - GUI -1 Lecture 3 - Graphical User Interfaces r GUI toolkits in Java API r JFrame r GUI components.
CompSci 100E 35.1 Graphical User Interfaces: GUIs  Components  Flat Layouts  Hierarchical Layouts  Designing a GUI  Coding a GUI.
Object Oriented programming Instructor: Dr. Essam H. Houssein.
Layout Manager Summary
Object Oriented Programming Engr. M. Fahad Khan Lecturer, Software Engineering Department University of Engineering & Technology, Taxila.
Applets and Frames. Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L14: GUI Slide 2 Applets Usually.
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.
3461 Laying Out Components Interior Design for GUIs.
Introduction to Java Chapter 9 - Graphical User Interfaces and Applets1 Chapter 9 Graphical User Interfaces and Applets.
Csc Basic Graphical User Interface (GUI) Components.
Computer Science 209 GUIs Model/View/Controller Layouts.
Basics of GUI Programming Chapter 11 and Chapter 22.
Creating a Window. A basic window in Java is represented by an object of the class Window in the package java.awt.
Ajmer Singh PGT(IP) JAVA IDE Programming - I. Ajmer Singh PGT(IP) GUI (Graphical User Interface) It is an interface that uses a graphic entities along.
AWT Layout Managers (Chapter 10) Java Certification Study Group January 21, 1999 Mark Roth.
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 Layouts CSIS 3701: Advanced Object Oriented Programming.
Java Swing - Lecture 3 Layout Management
1 CSE 331 Composite Layouts; Decorators slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D. Notkin, R. Mercer, Wikipedia
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
Graphical User Interface (GUI)
Introduction to GUI in 1 Graphical User Interface 3 Nouf Almunyif.
Introduction to GUI Programming in Java: Frames, Simple Components, and Layouts.
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.
AWT Vs SWING. 2 AWT (Abstract Window Toolkit) Present in all Java implementations Described in most Java textbooks Adequate for many applications Uses.
GUI.1 Graphical User Interfaces GUIs. GUI.2 The Plan Components Flat Layouts Hierarchical Layouts Designing a GUI Coding a GUI.
Chapter 7 A First Look at GUI Applications Layout Managers.
Graphical User Interfaces
Christopher Budo, Davis Nygren, spencer franks, Luke miller
Swing JComponents.
GUIs Model/View/Controller Layouts
Modern Programming Language Java
University of Central Florida COP 3330 Object Oriented Programming
Containers and Components
Creating Graphical User Interfaces
Steps to Creating a GUI Interface
Graphical User Interface
Presentation transcript:

University of Limerick1 Software Architecture Java Layout Managers

University of Limerick2 Lecture Objectives u Understanding the difference between Containers and Components u Understand what a Layout Managers is

Frames and components menus labelswindows (frames) checkboxes radio buttons borders buttons

Containers and Components u 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 u These classes inherit from (J)Component or Container respectively

Containers u Containers are Swing components that hold other components u Containers can be nested (containers in containers) u Containers use a LayoutManager to determine how to arrange the components u A Swing frame hold a container in it’s content pane

University of Limerick6 Example Containers u 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. u Window –A Window object is a top-level window with no borders and no menubar. u JFrame –A Frame is a top-level window with title and border; default layout is BorderLayout. u JDialog –A window that takes input from the user.

Swing: frame structure JFrame title bar content pane

More on Containers (1) u Swing provides four useful top-level container classes: JFrame, JApplet, JDialog & JWindow. u JPanel’s are intermediate containers. They are used to store other GUI objects, such as buttons, labels, textfields, etc. u Note there are other intermediate containers, such as TabbedPane, Toolbar, etc.

Components and Containers u GUI Objects such as labels, buttons, textfields, radiobuttons are consider atomic components…they DO NOT store other GUI objects within themselves. u Every top-level container contains an intermediate container, known as a content pane.

University of Limerick10 Components u Standard GUI controls like buttons, menus, scrollbars, text fields and lists are typical components u Swing also supplies some complex controls like Color, File and Font choosers, spreadsheet-like tables and Windows Explorer-like hierarchical trees

More on Components u So whenever you add an atomic component or intermediate container to a top-level container (e.g. JFrame), add it to the content pane. u E.g. Incorrect –add(new JButton(“hello”)); u Correct –getContentPane().add(new JButton(“hello”));

University of Limerick12 Adding Components to Containers u Containers exist to hold components which create the UI experience for the user u 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);

University of Limerick13 Layout managers u Using a layout manager you can choose one of a small number of standard arrangments for subwindows u 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 u you associate this layout manager with the window

Arranging components containers components

Example JPanel 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);

Why Layout Managers u So the programmer no longer bares the burden of specifying the exact position and dimension of each component

Layout Managers u each container manages layout of its components u the programmer just adds components, the container looks after layout u the container uses a Layout Manager to handle the layout u different layout managers are available u layout can be specified by specifying layout managers for containers

Layout Managers u Layout managers –FlowLayout –BorderLayout –GridLayout –GridBagLayout –CardLayout

FlowLayout u Arranges all components in a horizontal row u Components flow into next line when no space in row u The FlowLayout managers always honours a components preferred size u Default layout for JPanels

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);

BorderLayout u Content panes (main containers in JFrame, JDialog, JApplet) use BorderLayout by default) u Holds up to five components in fixed places center west east south north

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

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);...

GridLayout u Arranges components in a grid u The GridLayout managers ignores a components preferred size –Components are expanded to fill the sector

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);

CardLayout u Arranges components in time rather than space u Similar to a tabbed panel without the tabs u Components are arranged in sequence and only one is displayed at a time –N.B. A component may be a JPanel

GridBag Layout u Most powerful layout manager u Divides layout into an array of cell but cell may have different height and widths u Considered the most difficult to use

Layout Managers u By default every container has a layout manager u JPanel use FlowLayout by default u Content panes (main containers in JFrame, JApplet) use BorderLayout by default) u To override the default layout for any containers use the setLayout method or specify it during constructor u Remember for top level containers use getContentPane().setLayout(...).

Combining Layout managers u Most moderately sophisticated UI designs are too complex for the layout requirements to be satisfied by a single layout manager u So you will often need to combine two or more separate layout managers to achieve the desired affect overall

Combining Layout managers u This is accomplished by using extra container objects (JPanels) with different layout managers within a main container like a JFrame u It is also possible to specify that you do not want to employ a layout manager, and position subwindows yourself

University of Limerick31 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 to existing panel Set layout for panel