LAYOUT MANAGERS. Layout Managers Page 1. java.lang.object. java.awt.component. java.awt.container. java.awt.window. java.awt.panel



Similar documents
The Abstract Windowing Toolkit. Java Foundation Classes. Swing. In April 1997, JavaSoft announced the Java Foundation Classes (JFC).

Graphical User Interfaces

Mouse Event Handling (cont.)

How To Program In Java (Ipt) With A Bean And An Animated Object In A Powerpoint (For A Powerbook)

GUIs with Swing. Principles of Software Construction: Objects, Design, and Concurrency. Jonathan Aldrich and Charlie Garrod Fall 2012

Course/Year W080/807 Expected Solution Subject: Software Development to Question No: 1

How To Build A Swing Program In Java.Java.Netbeans.Netcode.Com (For Windows) (For Linux) (Java) (Javax) (Windows) (Powerpoint) (Netbeans) (Sun) (

GUI Components: Part 2

How to Convert an Application into an Applet.

Swing. A Quick Tutorial on Programming Swing Applications

Java is commonly used for deploying applications across a network. Compiled Java code

Fachbereich Informatik und Elektrotechnik Java Swing. Advanced Java. Java Swing Programming. Programming in Java, Helmut Dispert

INTRODUCTION TO COMPUTER PROGRAMMING. Richard Pierse. Class 7: Object-Oriented Programming. Introduction

Programming with Java GUI components

public class Craps extends JFrame implements ActionListener { final int WON = 0,LOST =1, CONTINUE = 2;

MiG Layout Quick Start Guide v Adding Components to the Grid. Merging and Splitting Cells

5.17 GUI. Xiaoyi Jiang Informatik I Grundlagen der Programmierung

Android Basic XML Layouts

JIDE Action Framework Developer Guide

Using A Frame for Output

LaGuardia Community College Thomson Ave, Long Island City, New York Created by ISMD s Dept. Training Team. Overview

Konzepte objektorientierter Programmierung

Sample Table. Columns. Column 1 Column 2 Column 3 Row 1 Cell 1 Cell 2 Cell 3 Row 2 Cell 4 Cell 5 Cell 6 Row 3 Cell 7 Cell 8 Cell 9.

Java GUI Programming. Building the GUI for the Microsoft Windows Calculator Lecture 2. Des Traynor 2005

Create Charts in Excel

Simple Line Drawing. Description of the Simple Line Drawing program

Ad Hoc Reporting. Usage and Customization

Access II 2007 Workshop

1 Hour, Closed Notes, Browser open to Java API docs is OK

Creating Web Pages with Microsoft FrontPage

Joomla Article Advanced Topics: Table Layouts

Tutorial: Time Of Day Part 2 GUI Design in NetBeans

Access 2007 Creating Forms Table of Contents

Tips and Tricks for Printing an Excel Spreadsheet

Java GUI Programming

@ - Internal # - Online TH PR OR TW TOTAL HOURS # @ 175

Using NetBeans IDE for Desktop Development. Geertjan Wielenga

WYSIWYG Editor in Detail

Microsoft Office PowerPoint Creating a new presentation from a design template. Creating a new presentation from a design template

method is never called because it is automatically called by the window manager. An example of overriding the paint() method in an Applet follows:

New Perspectives on Creating Web Pages with HTML. Considerations for Text and Graphical Tables. A Graphical Table. Using Fixed-Width Fonts

Create a Poster Using Publisher


Microsoft Excel Basics

Spreadsheet - Introduction

Custom Reporting System User Guide

Excel 2003 Tutorials - Video File Attributes

Microsoft Access 2010 handout

Excel Pivot Tables. Blue Pecan Computer Training Ltd - Onsite Training Provider :: :: info@bluepecan.co.

Declarative API for Defining Visualizations in Envision

Adobe Dreamweaver CC 14 Tutorial

HTML Lesson 7. Your assignment:

Getting Started with Excel Table of Contents

Advanced Word for Windows

Word 2003 Tables and Columns

Style & Layout in the web: CSS and Bootstrap

How To Write A Program For The Web In Java (Java)

Create and Print Your Own Greeting Cards

Excel 2007 A Beginners Guide

How to extend WindowBuilder to support new components. 1. Add component to palette Palette contribution in project

CREATING EXCEL PIVOT TABLES AND PIVOT CHARTS FOR LIBRARY QUESTIONNAIRE RESULTS

Working with the new enudge responsive styles

Microsoft Excel 2010 Tutorial

Catalog Creator by On-site Custom Software

Microsoft FrontPage 2003

Tutorial Creating a regular grid for point sampling

understand how image maps can enhance a design and make a site more interactive know how to create an image map easily with Dreamweaver

Scientific Graphing in Excel 2010

This document will describe how you can create your own, fully responsive. drag and drop template to use in the creator.

Tutorial 5. Working with Web Tables

Excel 2007 Basic knowledge

Excel 2003 Tutorial I

JOOMLA 2.5 MANUAL WEBSITEDESIGN.CO.ZA

ADOBE DREAMWEAVER CS3 TUTORIAL

Introduction to Microsoft Word 2008

All V7 registers support barcode printing, except the Sharp 410/420 1A ROM and that limitation is based upon the register.

PowerPoint: Graphics and SmartArt

DIGITAL DESIGN APPLICATIONS Word Exam REVIEW

Objectives. Understand databases Create a database Create a table in Datasheet view Create a table in Design view

Excel Project Creating a Stock Portfolio Simulation

Basic Microsoft Excel 2007

JIDE Docking Framework Developer Guide

DESIGN A WEB SITE USING PUBLISHER Before you begin, plan your Web site

Excel 2007 Tutorials - Video File Attributes

Excel 2003 A Beginners Guide

PowerPoint 2007 Basics Website:

To launch the Microsoft Excel program, locate the Microsoft Excel icon, and double click.

Using Clicker 5. Hide/View Explorer. Go to the Home Grid. Create Grids. Folders, Grids, and Files. Navigation Tools

Creating Web Pages with HTML Simplified. 3rd Edition

Using Microsoft Word. Working With Objects

Microsoft Access 2010 Overview of Basics

Tutorials. If you have any questions, comments, or suggestions about these lessons, don't hesitate to contact us at

Visualization Quick Guide

Microsoft Word defaults to left justified (aligned) paragraphs. This means that new lines automatically line up with the left margin.

Dreamweaver. Links and Tables

Currency Spending on durable medical equipment. Equipment Non-Durable Currency Spending on non-durable medical equipment.

Transcription:

LAYOUT MANAGERS A layout manager controls how GUI components are organized within a GUI container. Each Swing container (e.g. JFrame, JDialog, JApplet and JPanel) is a subclass of java.awt.container and so has a layout manager that controls it. java.lang.object java.awt.component java.awt.container java.awt.window java.awt.panel javax.swing.jcomponent java.awt.frame java.awt.dialog java.applet.applet javax.swing.jpanel javax.swing.jframe javax.swing.jdialog javax.swing.japplet Layout Managers Page 1

Flow Layout The simplest layout manager is java.awt.flowlayout, which adds components to the container from left-to-right, top-to-bottom. It is the default layout for GUI container objects of classes Applet or JPanel. Example Assume that an application has built a window with the following code: JFrame win = new JFrame( "Layout Demo" ); win.setdefaultcloseoperation( JFrame.EXIT_ON_CLOSE ); The code below builds a red label. By default, labels are transparent, so we must make it opaque for the color to show. The displayed label is shown at right. JLabel east = new JLabel( "EAST" ); east.setopaque( true ); east.setbackground( Color.RED ); Similarly, the code below makes four additional labels of varying colors: JLabel west = new JLabel( "WEST" ); west.setopaque( true ); west.setbackground( Color.BLUE ); JLabel north = new JLabel( "NORTH" ); north.setopaque( true ); north.setbackground( Color.GREEN ); JLabel south = new JLabel( " SOUTH" ); south.setopaque( true ); south.setbackground( Color.YELLOW ); JLabel center = new JLabel( " CENTER" ); center.setopaque( true ); center.setbackground( Color.ORANGE ); Using a flow layout manager for the window, the following code adds the five labels to the window. win.setlayout( new FlowLayout( ) ); win.add( east ); Layout Managers Page 2

win.add( west ); win.add( center ); win.add( north ); win.add( south ); Flow layout adds the labels in the order specified by the code, left to right, top to bottom. The completed window is shown to the right. The flow layout manager makes each component no larger than it needs to be to display its contents. Border Layout Another simple layout manager is java.awt.borderlayout, which is the default layout for GUI container objects of classes JFrame or JDialog. Border layout splits the GUI container into five areas east, west, north, south and center oriented so that north is the top. In the call to the add method, you must specify to which area of the container the component is to be added, otherwise it is added to the center area by default. Example Border layout adds the labels to the container areas specified by the call to meth add. The window build by the code below is shown to the right. The border layout manager expands the size of the component to fit its area. win.setlayout( new BorderLayout( ) ); win.add( east, BorderLayout.EAST ); win.add( west, BorderLayout.WEST ); win.add( center, BorderLayout.CENTER ); win.add( north, BorderLayout.NORTH ); win.add( south, BorderLayout.SOUTH ); Layout Managers Page 3

Grid Layout The third simple layout manager is java.awt.gridlayout, which adds components to a grid in row-major order (i.e. left to right and top to bottom). The class has a choice of three constructors: Constructors for java.awt.gridlayout GridLayout( int r, int c ) // Build a grid with r rows and c columns. GridLayout( ) // Build a grid with 1 row and a column for each added // component. GridLayout( int r, int c, int xgap, int ygap ) // Build a grid with r rows and c columns. 'xgap' is the spacing // between columns and 'ygap' is the spacing between rows. Example Grid layout adds the labels to the container using the grid specified by the call to its constructor. The window build by the code below is shown to the right. Grid layout makes each component in the grid equal sized. win.setlayout( new GridLayout( 2, 3, 6, 12 ) ); win.add( east ); win.add( west ); win.add( center ); win.add( north ); win.add( south ); Layout Managers Page 4

Box Layout javax.swing.boxlayout adds components to the container either vertically or horizontally. The components do not wrap as with flow layout. For example, a horizontal arrangement stays horizontal even when the container is resized. Here is the class constructor: BoxLayout( Container container, int orientation ) For the first argument, pass the content pane of the container that is to use the box layout. The second argument is an integer code indicating vertical or horizontal orientation. The BoxLayout class provides ease-of-use constants for this argument: Two of the Orientation Constants for BoxLayout X_AXIS Lay components out horizontally from left to right. Y_AXIS Lay components out vertically from top to bottom. Example The window build by the code below is shown to the right. Container box = win.getcontentpane( ); box.setlayout( new BoxLayout( box, BoxLayout.X_AXIS ) ); win.add( east ); win.add( west ); win.add( center ); win.add( north ); win.add( south ); Java provides the class javax.swing.box, which allows you to build a container whose layout manager is box layout. Most GUI programmers use this instead of javax.swing.boxlayout because it provides handy methods for configuring the box. For more information, see the topic javax.swing.box. Layout Managers Page 5

Grid Bag Layout java.awt.gridbaglayout allows very sophisticated placement of components within a container. It s essentially a grid, but the grid rows and columns may have varying heights and widths and components may span more than one grid cell. Consequently, it s a rather challenging layout manager to learn and use. If you wish to tackle it, see How to Use GridBagLayout in The Java Tutorials (google java tutorial gridbaglayout). Other Layout Managers javax.swing.grouplayout and javax.swing.springlayout were created to be used by GUI builder tools such as that found in NetBeans, although you can use them manually. See How to Use GroupLayout and How to Use SpringLayout in The Java Tutorials. Layout Managers Page 6

Exercises 1. Using the code from the first example, write a complete Java program (application or applet) that uses a flow layout manager to build and display the GUI shown to the right. 2. Modify your solution to exercise #1 so that it uses a border layout manager to build and display the GUI shown to the right. 3. In your solution to exercise #2, remove the second parameter from the first two calls to the frame s add method. Recompile and rerun the application. Observe the displayed GUI. Explain its appearance. 4. Modify your solution to exercise #1 so that it uses the two-parameter GridLayout constructor to build a grid layout manager that displays the GUI shown to the right. 5. In your solution to exercise #4, replace the two-parameter GridLayout constructor by the four-parameter constructor. The program should display the GUI shown to the right. The grid has vertical padding of 10 and horizontal padding of 5. 6. In your solution to exercise #4, replace the two-parameter GridLayout constructor by the constructor with no parameters. Recompile and rerun the program. Observe the displayed GUI. Explain its appearance. Layout Managers Page 7

7. Modify your solution to exercise #1 so that it uses the grid layout manager to build and display the GUI shown to the right. 8. Modify your your solution to exercise #1 so that it uses the box layout manager to build a horizontally oriented GUI as shown to the right. 9. Modify your your solution to exercise #1 so that it uses the box layout manager to build a vertically oriented GUI as shown to the right. Layout Managers Page 8