Graphic User Interfaces Layout Managers Event Handling.

Slides:



Advertisements
Similar presentations
Event handling and listeners What is an event? user actions and context event sources and listeners Why should my programs be event- driven? User interaction.
Advertisements

G5BUID - Java Swing Laying out components Manage realized components Determine size and position Each container has a layout manager (usually)
Java GUI building with the AWT. AWT (Abstract Window Toolkit) Present in all Java implementations Described in (almost) every Java textbook Adequate for.
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
Graphical User Interfaces Java’s AWT and Swing APIs.
CS18000: Problem Solving and Object-Oriented Programming.
Unit 3 Graphical User Interface (GUI) Dr. Magdi AMER.
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.
Event Driven Programming and GUIs Part 3 CS221 – 4/15/09.
Graphical User Interfaces (GUIs) GUI: An application that uses graphical objects to interact with users GUI applications consist of: –Events: A user or.
Chapter 121 Window Interfaces Using Swing Chapter 12.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 14 GUI and Event-Driven Programming.
1 GUI Elements in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
© The McGraw-Hill Companies, 2006 Chapter 10 Graphics and event- driven programs.
Chapter 13: Advanced GUIs and Graphics J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
Java Programming Chapter 10 Graphical User Interfaces.
Chapter 13 Advanced GUIs and Graphics. Chapter Objectives Learn about applets Explore the class Graphics Learn about the class Font Explore the class.
Java Software Solutions Lewis and Loftus Chapter 10 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Graphical User Interfaces --
Java Programming: From Problem Analysis to Program Design, 4e Chapter 12 Advanced GUIs and Graphics.
Applets and Frames CS 21a: Introduction to Computing I First Semester,
Java GUIs and Graphics CNS Outline  Introduction  Events  Components  Layout managers  Drawing  Introduction  Events  Components  Layout.
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.
More Event Handling Adapters Anonymous Listeners Pop menus Validating User Input.
Java Programming: Advanced Topics 1 Common Elements of Graphical User Interfaces Chapter 6.
 2002 Prentice Hall, Inc. All rights reserved Introduction Graphical User Interface (GUI) –Gives program distinctive “look” and “feel” –Provides.
– Advanced Programming P ROGRAMMING IN Lecture 21 Introduction to Swing.
1 Outline 1 Introduction 2 Overview of Swing Components 3 JLabel 4 Event Handling 5 TextFields 6 How Event Handling Works 7 JButton 8 JCheckBox and JRadioButton.
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
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.
Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006 by Maria Litvin, Gary Litvin, and Skylight.
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
Layout Managers Arranges and lays out the GUI components on a container.
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 Programming using NetBeans. RHS – SOC 2 GUI construction We have previously talked about elements in a (simple) GUI –Frames, Panes and Dialogs –Text.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.
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.
Java GUI. Graphical User Interface (GUI) a list a button a text field a label combo box checkbox.
Introduction to Java Chapter 9 - Graphical User Interfaces and Applets1 Chapter 9 Graphical User Interfaces and Applets.
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.
1 Layout Managers Layout managers –Provided for arranging GUI components –Provide basic layout capabilities –Processes layout details –Programmer can concentrate.
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
Graphical User Interface (GUI)
1 Lecture 8: User Interface Components with Swing.
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.
CSC 205 Programming II Lecture 7 AWT – Event Handling & Layout.
Introduction to GUI Programming in Java: Frames, Simple Components, and Layouts.
5-1 GUIs and Events Rick Mercer. 5-2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces to respond.
CIS 270—Application Development II Chapter 11—GUI Components: Part I.
GUIs & Event-Driven Programming Chapter 11 Review.
Chapter 7 A First Look at GUI Applications Layout Managers.
A Quick Java Swing Tutorial
Graphical User Interface (pronounced "gooey")
Ellen Walker Hiram College
Chap 7. Building Java Graphical User Interfaces
Graphical User Interfaces -- Introduction
Containers and Components
Creating Graphical User Interfaces
Panels & Layout Managers
Steps to Creating a GUI Interface
Layout Organization and Management
Graphical User Interface
Presentation transcript:

Graphic User Interfaces Layout Managers Event Handling

Why Do we need Layout Mgrs? What could happen… What should happen… resize

Layout Managers A Layout manager encapsulates an algorithm for positioning and sizing of GUI components –they control the positioning of the components in the container A layout manager must be associated with a Container object to perform its work –layout manager determines the positioning of the components on the Container –JPanel panel = new JPanel(); panel.setLayout(new BorderLayout()); –JPanel panel = new JPanel(new BorderLayout());

Layout Managers LayoutManager is an interface that is implemented by a number of existing classes awt layout managers –FlowLayout –BorderLayout –GridLayout –CardLayout –GridBagLayout –Nice explanation of each type at ox. Look at them!

Borderlayout this layout divides the container into 5 regions, centre, N, S, E and W

Border Layout –arranges components to fit 5 regions, centre, N, S, E and W –default for Java application content pane –can only add one component to each region, so use JPanels pane.add(component, position) BorderLayout.NORTH BorderLayout.SOUTH BorderLayout.EAST BorderLayout.WEST BorderLayout.CENTER

Grid layout

Flow Layout

–arranges components left to right –components are centered by default, but can be changed –Constructors FlowLayout() FlowLayout(int align) FlowLayout(int align, int hdist, int vdist) FlowLayout.RIGHT FlowLayout.LEFT FlowLayout.CENTER C1 C2 C3 C4 C5 C6 vdist hdist

Using Layout Managers You typically have several layouts on a single screen E.g. Flowlayout for a panel containing Cancel/OK buttons Border layout for the content pane of your frame (Add the panel for the cancel, ok buttons to south border region of your frame… What does this look like? How would you get the buttons down into the bottom right corner? To use layouts: assign a layout for whatever is getting a layout (panel for buttons, a frame etc) Then.. Add the individual “things” – specifying the location if necessary (Border layout requires n,s,e,w)

Using Layout Managers private Container createContentPane(){ // set up the content pane to use BorderLayout JPanel cPane = new JPanel(new BorderLayout()); // create panel for text fields JPanel fieldsPanel = new JPanel(); fieldsPanel.setLayout(new FlowLayout()); // add components to panel fieldsPanel.add(new JLabel("Please enter your name: ")); JTextField name= new JTextField("here....", 10); fieldsPanel.add(name); // add panel to center of frame cPane.add(fieldsPanel, BorderLayout.CENTER); // create and add button to bottom of frame ok= new JButton("OK"); cPane.add(ok, BorderLayout.SOUTH); return cPane; } 2 ways to set layout What will this look like ?

Grid Layout –divided into rows and cols GridLayout(int rows, int cols) –each component is given the same size and positioned left to right –you fix the number of rows or cols required, e.g. panel.setLayout(new GridLayout(3,0)) “any number of” a 2 x 4 grid for 5 components will create a 2 x 3 grid (unless you fill the empty ones with spaces

Card layout CardLayout –overlapping panels - same display space –Only one is displayed at a time

More..Layouts BoxLayout –like a Flow with no overlapping, –Single row or column –can be arranged horizontally or vertically Box class –Container that uses a BoxLayout manager –easier than creating a panel and allocating the BoxLayout –Box box = Box.createHorizontalBox(); setContentFrame(box); –Use “glue” and “struts” to maintain sizes when resizing

GridBaglayout GridBagLayout –flexible and sophisticated –place components in a grid of cells –components can span more than one cell –rows can have different widths and columns can have different heights –Avoid!!

Spring layout SpringLayout –flexible –specify precise relationships between edges of components

Event Driven Programming

Developing a GUI uses event driven programming waits for input user input TraditionalEvent Driven

Event Driven Programming Model User interacts with application generating events –pushing mouse button, scrolling, clicking on checkboxes, moving mouse,… Event is trapped and depending on the type of event an appropriate event handler executes Swing event handling and painting executes in a single thread called the event-dispatching thread –ensures each event handler finishes executing before next starts –prevents deadlock

Handling User Interaction Events are triggered by user interaction with components Events are captured by listeners –if and only if the listener is registered with the appropriate component Listener has an event handler that handles the captured event events and listeners are objects event handlers are methods of the Listener object Button ActionListener ActionEvent actionPerformed() invokes

Listeners Listeners are interfaces –Any object can be a listener… Listener objects capture events that occur on components that they are registered with Each Event class has an associated Listener interface Listener objects should implement appropriate event handlers –ActionEvent  ActionListener  actionPerformed() for button presses … –TextEvent  TextListener  textValueChanged() for changing text in text fields

User Interaction Create the appropriate listener object –implement the appropriate listener interface Implement event handler methods for all events that are important –event handler methods are the listener abstract methods that must be implemented to implement the interface Register the listener with the control (component) to allow it to receive events

Interacting with a Button Event is clicking on a button Create listener –appropriate interface is ActionListener –create listener object that implements ActionListener –e.g. class MyActionListener implements ActionListener {…} –instantiate: MyActionListener myListener = new MyActionListener(); Note: there are several ways of creating the listener – more later. Implement event handler – include actionPerformed() method in class that implements ActionListener – class MyActionListener implements ActionListener { … public void actionPerformed(ActionEvent e){…} … } Register listener with button component myButton = new myButton(“whatever”); myButton.addActionListener(myListener);

Let your window be the Listener public class MyWindow extends JFrame implements ActionListener { public MyWindow(){... ok = new JButton("OK"); ok.addActionListener(this); panel.add(ok);... } // event handler public void actionPerformed (ActionEvent e){ System.exit(0); } create Listener register it with the component include an event handler

Listener Interfaces… User action that results in EventListener that handles it User clicks a button, presses Enter whileActionListener typing in a text field, chooses a menu item User closes a frame (main window)WindowListener User presses a mouse button while overMouseListener a Component User moves the mouse over a componentMouseMotionListener Component gets the keyboard focusFocusListener Table or list selection changesListSelectionListener Any property in a component changes,PropertyChangeListener such as the text on a button

Summary Last topic allowed - set up a frame with components This topic gives you enough info to.. –Specify a layout for the content (and various panels) within your window –“do something” when a component is used (e.g. Button click) Implement a listener interface e.g. –public class MyWindow extends JFrame implements ActionListener Add the listener to a component e.g. –ok = new JButton("OK"); ok.addActionListener(this); Implement the method for what should happen e.g. –public void actionPerformed (ActionEvent e){... Some code that does something..