Containers and Components

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
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.
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
Event Driven Programming and GUIs Part 3 CS221 – 4/15/09.
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.
Swing CS-328 Dick Steflik John Margulies. Swing vs AWT AWT is Java’s original set of classes for building GUIs Uses peer components of the OS; heavyweight.
LAB SESSION 10 *LAYOUT MANAGER *LISTENERS. Laying the components manually by using a null layout is tedious. Each container object has a layout manager.
Jan Containers Yangjun Chen Dept. Business Computing University of Winnipeg.
1 GUI Elements in Java Nelson Padua-Perez Chau-Wen Tseng 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.
OOP (Java): Layout/ OOP Objectives – –describe the basic layout managers for GUIs Semester 2, GUI Layout.
Layout Managers A layout manager is an object that determines the way that components are arranged in a container There are several predefined layout managers.
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.
Layout Management Containers can arrange their components. Our container is a JPanel, and it can set the way it’s components will be laid out : mypanel.setLayout.
Applets and Frames CS 21a: Introduction to Computing I First Semester,
CSE 219 Computer Science III Graphical User Interface.
Rina System development with Java Instructors: Rina Zviel-Girshin Lecture 12.
Java Programming: Advanced Topics 1 Common Elements of Graphical User Interfaces Chapter 6.
1 Why layout managers Can we perform layout without them? –Yes. A container’s layout property can be set to null. Absolute positioning: specify size and.
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.
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
Layout Manager Summary
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.
University of Limerick1 Software Architecture Java Layout Managers.
3461 Laying Out Components Interior Design for GUIs.
Java layout managers. import java.awt.*; import java.awt.event.*; import javax.swing.*; class ButtonPanel extends JPanel implements ActionListener { public.
J McQuillanSE204:2004/2005: Lecture 3Slide 1 Specialised Components Can create specialised components. Do this by subclassing the component that you are.
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.
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.
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.
1CS480: Graphical User Interfaces. Dario Salvucci, Drexel University. Lecture 3: Layout Basics.
Event Handler Methods Text field Object Responder JAVA AWT Environment: Messages are sent between JAVA Objects Screen Event Notification Press Button.
User Interface Components. Layout Placement of UI components in a window – Size & Position Each component occupies a rectangular region in the window.
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 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.
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
Tim McKenna Layout Mangers in Java Tim McKenna
Chap 7. Building Java Graphical User Interfaces
Layout Managers A layout manager is an object that determines the way that components are arranged in a container There are several predefined layout managers.
Graphical User Interfaces -- Introduction
Timer class and inner classes
תכנות מכוון עצמים בשפת JAVA
Tim McKenna Layout Mangers in Java Tim McKenna
Creating Graphical User Interfaces
Panels & Layout Managers
Steps to Creating a GUI Interface
GUI Layouts By: Leonard & Saif.
Layout Organization and Management
Graphical User Interface
Presentation transcript:

Containers and Components A GUI has many components in containers. A container contains other components. A container is also a component; so a container may contain other containers. add(component) container component component add(component) add(...) container container component component component component

Layout Managers A container uses a Layout Manager to manage the position and size of components. Container layoutManager contains * Component layout components LayoutManager

Why a layout manager? Demo: compare a Java application and Visual C# application when resizing a window. In Java, the layout manager will rearrange or resize components. In Visual C#, the components disappear.

Layout Managers Classic Layout Managers are: BorderLayout (default for JFrame) FlowLayout (default for JPanel) BoxLayout GridLayout GridBagLayout CardLayout SpringLayout

add Layout Manager Use setLayout( ) to assign a Layout Manager: frame.setLayout( new FlowLayout( ) ); panel.setLayout( new GridLayout(3,4) );

Customizing a Layout Some layout managers give you control over the layout. FlowLayout, BorderLayout - set space between components GridBagLayout - almost unlimited control static final int GAP = 8; FlowLayout layout = new FlowLayout( ); layout.setVGap( GAP ); layout.setHGap( GAP );

BorderLayout BorderLayout divides the container into 5 zones. use: container.add( component , WHERE ); If a zone is not used, other zones expand to use the space. CENTER gets the most space. NORTH CENTER WEST EAST SOUTH JButton button1 = new JButton("Button 1"); frame.add( button1, BorderLayout.EAST );

FlowLayout "flows" the components into the available space. preserves the original (or requested) size of each component. components are added left to right, in order. container.setLayout( new FlowLayout( ) ); JButton button1 = new JButton( "Button 1" ); JButton button2 = new JButton( "Button 2" ); container.add( button1 ); container.add( button2 );

CardLayout CardLayout lets you have different sets of components displayed at different times. One card is displayed at a time. Use next( ), first(), last() to change the displayed card. frame.setLayout( new CardLayout( ) ); // add buttons and panels to the cards contentpane.add( button1 ); contentpane.add( button2 );

GridLayout specify a grid size (rows,cols) in constructor. Components are added to a grid, in the order that they are added. GridLayout makes all components have same size. frame.setLayout( new GridLayout(3, 2) ); // (rows,cols) // add buttons to the grid frame.add( button1 ); frame.add( button2 ); frame.add( button3 );

BoxLayout BoxLayout puts components in a single row or column. It does not resize components. It allows different forms of component alignment. container.setLayout( /* vertical box layout */ new BoxLayout(container, BoxLayout.Y_AXIS) ); container.add( button1 ); container.add( button2 );

GridBagLayout and SpringLayout GridBagLayout and SpringLayout give you more control over the layout and sizing of components. can control margins, free space distribution, etc.

Newer Layout Managers GroupLayout - treat several components as a group, so they can all have the same size or alignment. Makes layouts look much more professional. FormLayout - layout a form containing labels and input areas in rows and columns, nicely aligned. It provides build-in data validators. Layout can be specified in text instead of Java code. FormLayout is a free, open- source component from http://www.jgoodies.com/freeware/libraries/forms/ AbsoluteLayout - put everything exactly where you say (like in VisualStudio)

Have a JFrame or Be a JFrame? Two styles of defining a UI class using JFrame: public class SwingExample { private JFrame frame; ... public SwingExample( ) { frame = new JFrame( ); frame.setDefaultCloseOperation( ... ); public class SwingExample extemds JFrame { ... public SwingExample( ) { this.setDefaultCloseOperation( ... ); initComponents( ); "this" object is a JFrame. Don't create another one!!

BlueJ: example of nested containers Container with a row of buttons (buttons based on user prefs) text component Container with 2 components inside

Lightweight Containers A lightweight container is one that is not a window. You must place it inside another container. Cannot be drawn on screen by itself. JPanel simple rectangular area - most common JTabbedPane - multiple panels with a tab on top JSplitPane JInternalFrame - like a JFrame inside a JFrame

Nesting Containers Example: a panel containing a text field and a button JTextField textfield = new JTextField(12); JButton button = new JButton("Login"); JPanel panel = new JPanel( ); panel.add( textfield ); panel.add( button ); Nesting: put the JPanel inside a JFrame JFrame frame = new JFrame( ); frame.add( panel );

Benefit of LayoutManager What are the benefits of separating LayoutManager from the container classes? Why don't we put the layout code inside each container?