Java Layouts CSIS 3701: Advanced Object Oriented Programming.

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
GUI Elements Session 17. Memory Upload Layout Components Button TextField TextArea Label Choice Containers Panels The applet itself.
Graphical User Interfaces
TCU CoSc Programming with Java Visual Design (Chapter 5)
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
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 20, 2005.
Java Swing Recitation – 11/(20,21)/2008 CS 180 Department of Computer Science, Purdue University.
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.*;
LAB SESSION 10 *LAYOUT MANAGER *LISTENERS. Laying the components manually by using a null layout is tedious. Each container object has a layout manager.
CS102--Object Oriented Programming Lecture 19: – The Swing Package (II) Copyright © 2008 Xiaoyan Li.
1 GUI Elements in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
COMP 14 Introduction to Programming Miguel A. Otaduy June 7, 2004.
Layout Mangers CSC 171 FALL 2001 LECTURE 14. History: The Transistor William Shockley, John Bardeen, and Walter Brattain invent the transfer resistance.
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.
GUI and event-driven programming An introduction.
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.
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
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.
Timer class and inner classes. Processing timer events Timer is part of javax.swing helps manage activity over time Use it to set up a timer to generate.
Object Oriented programming Instructor: Dr. Essam H. Houssein.
Layout Manager Summary
1 GUIs, Layout, Drawing Rick Mercer. 2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces (GUIs)
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.
University of Limerick1 Software Architecture Java Layout Managers.
3461 Laying Out Components Interior Design for GUIs.
The Abstract Window Toolkit (AWT) supports Graphical User Interface (GUI) programming. AWT features include: a rich set of user interface components; a.
1 GUIs, Layout, Drawing Rick Mercer. 2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces (GUIs)
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 14 : Swing II King Fahd University of Petroleum & Minerals College of Computer Science.
Java layout managers. import java.awt.*; import java.awt.event.*; import javax.swing.*; class ButtonPanel extends JPanel implements ActionListener { public.
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.
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.
Lab 4: GUIs, Panels, Mouse and Key Listeners ICOM4015: FALL 2014 A N I NTRODUCTION TO P ANELS AND L AYOUTS CREATED BY KATYA I. BORGOS REVISED BY AMIR H.
March R. Smith - University of St Thomas - Minnesota QMCS 230: Today in Class Project Proposals: Due Today!Project Proposals: Due Today! –Identify.
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.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 7-3 ( Book Chapter 14) GUI and Event-Driven Programming.
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.
Applets. 9/04/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L12: Applets Slide 2 Applets Usually.
Introduction to GUI in 1 Graphical User Interface 3 Nouf Almunyif.
Introduction to GUI Programming in Java: Frames, Simple Components, and Layouts.
Dept. of CSIE, National University of Tainan 10/21/2012 Arranging Components on a User Interface.
Java Visual Applications CSIS 3701: Advanced Object Oriented Programming.
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.
Modern Programming Language Java
Tim McKenna Layout Mangers in Java Tim McKenna
Containers and Components
Tim McKenna Layout Mangers in Java Tim McKenna
Creating Graphical User Interfaces
Chapter 7-3 (Book Chapter 14)
GUI building with the AWT
Layout Organization and Management
Presentation transcript:

Java Layouts CSIS 3701: Advanced Object Oriented Programming

Adding Components to Surface ContentPane –Member variable representing visible surface of app –Accessed with getContentPan e method –Add JPanel to surface and add components to it Application ContentPane getContentPane() JPanel add

Adding Components to Surface // Create a new visual panel and add it to the // surface (the ContentPane) of the application. JPanel mainPanel = new JPanel(); getContentPane().add(mainPanel); // Add the button and textfield to // panel to make them visible. mainPanel.add(helloButton); mainPanel.add(responseField);

Component Positioning Positioning: Determining where/how components displayed on surface of JPanel –Location: x and y position (relative to top left corner of panel) –Size: width and height in pixels y x width height

Component Positioning Absolute Positioning –Manually setting x, y position and width, height –Often done in IDEs using “drag and drop” from “toolbox” Layout-based Positioning –Automatically setting position based on rules –Positions, sizes of components based on: Size of container Preferences about relative positions of components

Absolute Positioning Sizes of applications not fixed –Screen resolution –Browser propertied (if applet) –Manual user resizing Problem: Components may not be visible if application resized

Layout-based Positioning Automatically adapts to size of container –Shifting component positions –Resizing components –Depends on rules used by container layout Shifting components Resizing components

Layout Objects Layout objects: –Contain rules for determining where component placed on surface of panel –Contain rules for setting size of components –In java.awt.* package Key question: should component be natural size –Just large enough for text, etc. to be drawn on surface Natural sizes Sizes adjusted based on panel

Layout Objects Layouts are objects of different types –Must be constructed with desired parameters Passed to containers as parameters –Usually to constructor JPanel P = new JPanel(new FlowLayout()); –Can also later use setLayout method New layout object Passed to JPanel constructor

Layout Objects Layout objects used by container when new component added to surface JPanel object Layout object add(b) Where should I add b ? x, y position What should b ’s size be? width, height

FlowLayout Class Components added to container in “left to right” order –Kept on same “line” –“Auto wrapped” when reach edge of container –Centered on line Components kept natural size JPanel P = new JPanel(new FlowLayout()); P.add(b1); P.add(b2); P.add(b3); P.add(b4); P.add(b5); P.add(b6); b1b2 b3 b4 b6 b5

GridLayout Class Container divided into grid of cells of equal size –Rows, columns set in constructor Components added to container in “row major” order Components resized to fit cell –Text may be abbreviated if necessary JPanel P = new JPanel(new GridLayout(3, 2)); P.add(b1); P.add(b2); P.add(b3); P.add(b4); P.add(b5); P.add(b6); b1 b2 b3 b5b6 b4

BorderLayout Class Container divided into 5 areas Must specify location to place component in overloaded add method JPanel P = new JPanel(new BorderLayout()); P.add(b1, BorderLayout.SOUTH); P.add(b2, BorderLayout.WEST); P.add(b3, BorderLayout.NORTH); P.add(b4, BorderLayout.EAST); P.add(b5, BorderLayout.CENTER); Static constants in BorderLayout class used NORTH SOUTH WESTEASTCENTER

BorderLayout Class Sizing rules: –NORTH, SOUTH : natural height width of entire container –EAST, WEST : natural width height of container (minus NORTH, SOUTH ) –CENTER : leftover area after NORTH, SOUTH, EAST, WEST b1 b3 b2 b5b4

Layout Advantages/Disadvantages FlowLayout –Advantage: components natural size –Disadvantage: no guarantee components next to one another if “wrapped” GridLayout –Advantage: fixed component positions –Disadvatage: unnatural component sizes BorderLayout –Advantage: fixed component positions –Disadvatage: unnatural component sizes, only 5 positions

Layout Design Can nest panels inside one another JPanel mainPanel = new JPanel(new BorderLayout()); JPanel subPanel = new JPanel(new GridLayout(4,2)); mainPanel.add(subPanel, BorderLayout.EAST); Can choose layout for each panel to achieve best effects –Often give GridLayout single row or column –Often only use parts of BorderLayout subPanel mainPanel

Layout Design Example: “File loader” application Requirements: –“File” and text area must be adjacent and natural size –LOAD and SAVE buttons must be adjacent and same size –text area and button can be above or next to one another

Layout Design Create panel ( filePanel ) for “File” and text area –Use a BorderLayout with “File” to west and text in center Create panel ( buttonPanel ) for buttons –Use a GridLayout with one row and two columns Create panel ( mainPanel ) for subpanels –Use a FlowLayout for flexibility filePanel buttonPanel

Layout Design // Visual components fileField = new JTextField(15); loadButton = new JButton("LOAD"); saveButton = new JButton("SAVE"); // Main panel to which we add the subpanels JPanel mainPanel = new JPanel(new FlowLayout()); getContentPane().add(mainPanel); setSize(250, 100);

Layout Design // Subpanel for label and file textfield JPanel filePanel = new JPanel(new BorderLayout()); filePanel.add(new JLabel(" File: "), BorderLayout.WEST); filePanel.add(fileField, BorderLayout.CENTER); // Subpanel for buttons, which should be same size JPanel buttonPanel = new JPanel(new GridLayout(1, 2)); buttonPanel.add(loadButton); buttonPanel.add(saveButton); // Add the subpanels to the main panel mainPanel.add(filePanel); mainPanel.add(buttonPanel);

Absolute Positioning Can use absolute positioning in Java –Set layout of container to null JPanel P = new JPanel(null); –Add components as usual P.add(b1); P.add(b2); P.add(b3);

Absolute Positioning Use setBounds method to set position and size setBounds(int x, int y, int w, int h) b1.setBounds(10, 10, 80, 40); b2.setBounds(100, 10, 50, 40); b3.setBounds(10, 60, 200, 40);