Copyright © 2002, Systems and Computer Engineering, Carleton University. 94.204-18-Gui1.ppt 1 94.204* Object-Oriented Software Development Part 18 Building.

Slides:



Advertisements
Similar presentations
Unit 091 Introduction to GUI Programming Introduction to User Interfaces Introduction to GUI Programming GUI Design Issues GUI Programming Issues Java.
Advertisements

Event Handling. In this class we will cover: Basics of event handling The AWT event hierarchy Semantic and low-level events in the AWT.
Events ● Anything that happens in a GUI is an event. For example: – User clicks a button, presses return when typing text, or chooses a menu item ( ActionEvent.
Graphics Programming. In this class, we will cover: The difference between AWT and Swing Creating a frame Frame positioning Displaying information in.
Carnegie Mellon University, MISM1 Java GUI programming and Java Threads GUI example taken from “Computing Concepts with Java 2” by Cay Horstmann Thread.
Object Oriented Programming Java 1 GUI example taken from “Computing Concepts with Java 2” by Cay Horstmann GUI Programming.
Intermediate Java1 An example that uses inner classes Week Four Continued.
1 GUI Elements in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
1 lecture 12Lecture 13 Event Handling (cont.) Overview  Handling Window Events.  Event Adapters Revisited.  Introduction to Components and Containers.
Chapter 13: Advanced GUIs and Graphics J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
Scott Grissom, copyright 2006Ch 11: GUI Slide 1 Graphical User Interfaces (Ch 11) Careful design of a graphical user interface is key to a viable software.
More on Creating GUIs in Java using Swing David Meredith Aalborg University.
CC1007NI: Further Programming Week 5 Dhruba Sen Module Leader (Islington College)
Graphic User Interfaces Part 1. Typical GUI Screen from Microsoft Word What GUI “components” can you see? –Menus? Buttons? Labels? What else? –Anything.
Java Programming Chapter 10 Graphical User Interfaces.
1 Event Driven Programming wirh Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
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,
Graphical User Interface CSI 1101 N. El Kadri. Plan - agenda Graphical components Model-View-Controller Observer/Observable.
More Event Handling Adapters Anonymous Listeners Pop menus Validating User Input.
Java Programming: Advanced Topics 1 Common Elements of Graphical User Interfaces Chapter 6.
GUIs in Java Swing, Events CS2110, SW Development Methods Readings: MSD, Chapter 12 Lab Exercise.
Ch 3-4: GUI Basics Java Software Solutions Foundations of Program Design Sixth Edition by Lewis & Loftus Coming up: GUI Components.
1 CIS 421 Web Based Java Programming Building GUIs (Graphical User Interfaces) with Java Part 1.
1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers.
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.
1 Unit 5 GUI Aum Amriteshwaryai Namah. 2 Overview Shall learn how to reuse the graphics classes provided by Java for constructing Graphical User Interface.
GUI Clients 1 Enterprise Applications CE00465-M Clients with Graphical User Interfaces.
Graphics and Event-Driven Programming in Java John C. Ramirez Department of Computer Science University of Pittsburgh.
Copyright © 2002, Systems and Computer Engineering, Carleton University c-Gui3.ppt * Object-Oriented Software Development Part 18-c Building.
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.
Graphical User Interfaces (Part 2) 1. View  view  presents the user with a sensory (visual, audio, haptic) representation of the model state  a user.
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.
Pravin Yannawar, DOCS, NMU Jalgaon. Basic Java : Event handling in AWT and Swing 2 Objectives of This Session Explain the Event handling mechanism & demonstrate.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
OOP (Java): GUI Intro/ OOP Objectives – –use an image viewer application to introduce Java's GUI features Semester 2,
Creating Windows. How can we use Java to create programs that use windows (GUI applications)? How can we use Java to create programs that use windows.
Lec.10 (Chapter 8 & 9) GUI Jiang (Jen) ZHENG June 27 th, 2005.
Creating a GUI with JFC/Swing. What are the JFC and Swing? JFC –Java Foundation Classes –a group of features to help people build graphical user interfaces.
COSC 3461: Module 2 Control Flow Paradigms: Reacting to the User.
CS1054: Lecture 21 - Graphical User Interface. Graphical User Interfaces vs. Text User Interface.
Java GUI. Graphical User Interface (GUI) a list a button a text field a label combo box checkbox.
Anonymous Classes An anonymous class is a local class that does not have a name. An anonymous class allows an object to be created using an expression.
Object Oriented Programming.  Interface  Event Handling.
Ch13 Creating windows and applets. Short overview AWT (Abstract Windowing Toolkit) Early Java development used graphic classesEarly Java development used.
GUIs Graphical User Interfaces. Everything coming together Known: – Inheritance – Interfaces – Abstract classes – Polymorphism – Exceptions New: – Events.
CIS Intro to JAVA Lecture Notes Set 8 9-June-05.
Java the UML Way versjon Only to be used in connection with the book "Java the UML Way", by Else Lervik and.
Creating a Window. A basic window in Java is represented by an object of the class Window in the package java.awt.
Java Swing One of the most important features of Java is its ability to draw graphics.
1 Event Driven Programs with a Graphical User Interface Rick Mercer.
Advanced Java Class Events. change in state initiated by system or user java.util.EventObject java.awt.event java.swing.event.
CSI 3125, Preliminaries, page 1 Event Handling. CSI 3125, Preliminaries, page 2 Event Handling An Event Change in the state of an object is known as event.
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
Lesson 28: More on the GUI button, frame and actions.
Sep 181 Example Program DemoTranslateEnglishGUI.java.
MIT AITI 2004 Swing Event Model Lecture 17. The Java Event Model In the last lecture, we learned how to construct a GUI to present information to the.
5-1 GUIs and Events Rick Mercer. 5-2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces to respond.
1 Event Driven Programming with Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 7 ( Book Chapter 14) GUI and Event-Driven Programming.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 7 Event-Driven Programming and Basic GUI Objects.
CSC 205 Programming II Lecture 5 AWT - I.
CHAPTER Reacting to the user.
A First Look at GUI Applications
Ellen Walker Hiram College
Event-driven programming for GUI
Constructors, GUI’s(Using Swing) and ActionListner
Presentation transcript:

Copyright © 2002, Systems and Computer Engineering, Carleton University Gui1.ppt * Object-Oriented Software Development Part 18 Building Graphic User Interfaces with Java Part 1 revised March 20021

Copyright © 2002, Systems and Computer Engineering, Carleton University Gui1.ppt 2 Pedagogical Note We are going to focus on Java's Swing Toolkit (a set of classes for building GUI-based applications and applets) as an example of a fairly large class hierarchy that exploits the fundamental O-O concepts supported by Java (classes, objects, inheritance, polymorphism, abstract classes, interfaces) We won’t spend as much time on the older Abstract Windowing Toolkit (AWT); however, most of what you learn about Swing also applies to the AWT, which is still supported in Java 2, so by the end of the course you should be able to use both toolkits

Copyright © 2002, Systems and Computer Engineering, Carleton University Gui1.ppt 3 Pedagogical Note Many books (including some university-level texts) take an "applets-first" approach –anything to do with the World Wide Web (hence applets) is considered “must know” technology –these books show you how to build applets by example (i.e., what methods your code must provide, what methods your code can call), but they don't explain how applets work

Copyright © 2002, Systems and Computer Engineering, Carleton University Gui1.ppt 4 Pedagogical Note We'll start with applications, then show how applets fit into the picture In other words, instead of simply memorizing what we must do to create GUI-based applications & applets, we are now in a position to understand how & why they work

Copyright © 2002, Systems and Computer Engineering, Carleton University Gui1.ppt 5 java.awt - The Abstract Windowing Toolkit Introduced with Java 1.0 Classes are divided into 3 main categories: –graphics (colours, fonts, shapes, etc.) –components (GUI components: windows, buttons, menus, etc.) –layout managers (control the positioning of components on the screen) Each component corresponds to a “peer” component provided by the native GUI toolkit on the target platform (Windows, Sun Solaris, etc.) Here is a (small) subset of the AWT class hierarchy:

Copyright © 2002, Systems and Computer Engineering, Carleton University Gui1.ppt 6 Object Component Applet Container Frame Window Pane l

Copyright © 2002, Systems and Computer Engineering, Carleton University Gui1.ppt 7 java.awt - The Abstract Windowing Toolkit Component –an abstract class –superclass of all GUI components except menu components and class CheckboxGroup Container –the superclass for all components that contain other components –defines add(), for adding components to a container

Copyright © 2002, Systems and Computer Engineering, Carleton University Gui1.ppt 8 java.awt - The Abstract Windowing Toolkit Window –a top-level window with no border or menu bar –rarely instantiated (its subclasses are more useful) Frame –a window with a title bar –can have a menu bar –top-level window for Java AWT-based applications typically, main() creates an instance of Frame as its top-level application window, then adds GUI components to the frame

Copyright © 2002, Systems and Computer Engineering, Carleton University Gui1.ppt 9 java.awt - The Abstract Windowing Toolkit Panel –a container that must be contained within another container –does not have its own window Applet –a subclass of Panel –actually part of the java.applet package, not the AWT

Copyright © 2002, Systems and Computer Engineering, Carleton University Gui1.ppt 10 AWT Limitations “look and feel” of AWT-based programs differs slightly across platforms, because of differences in the underlying native GUI elements AWT components limited to those that are available on all platforms (lowest common denominator)

Copyright © 2002, Systems and Computer Engineering, Carleton University Gui1.ppt 11 javax.swing - The Swing Toolkit In response, Netscape developed the Internet Foundation Classes, which evolved into the Swing toolkit that is part of Sun’s Java Foundation Classes (JFC) Swing components do not require native peer components Each Swing UI component is painted onto a blank window Only peer functionality required is the ability to display windows and paint on them Here is a (small) subset of the Swing class hierarchy, showing its relationship to the AWT classes:

Copyright © 2002, Systems and Computer Engineering, Carleton University Gui1.ppt 12 Object Component Container Frame Window Applet Panel JFrame JApplet JPanel JComponent

Copyright © 2002, Systems and Computer Engineering, Carleton University Gui1.ppt 13 Example - Creating a Frame import javax.swing.*; public class FrameExample1 { public static void main(String args[]) { JFrame f = new JFrame("Frame Example 1"); f.setSize(400, 300); f.show(); }

Copyright © 2002, Systems and Computer Engineering, Carleton University Gui1.ppt 14 Example - Creating a Frame

Copyright © 2002, Systems and Computer Engineering, Carleton University Gui1.ppt 15 Example - Creating a Frame By default, a frame is sized at at 0x0 pixels, so setSize() is sent to the frame to change it size to 400 pixels wide x 300 pixels high If the setSize() message is not sent, only the title bar is displayed setSize() inherited from Component The show() message is sent to the frame to make it visible and bring it to the front (if it is behind another window) show() inherited from Window

Copyright © 2002, Systems and Computer Engineering, Carleton University Gui1.ppt 16 Example - A Closeable Frame This frame has a deficiency that limits its usefulness as a basis for developing applications –clicking on the Close button or selecting Close from the left-most drop-down menu hides the frame but does not close the application –if the Java interpreter was run from a command line, to stop the program we must close the console window (NT) or issue a kill command(Unix). This is because nobody is listening to the closing of the window…

Copyright © 2002, Systems and Computer Engineering, Carleton University Gui1.ppt 17 How Does a GUI-based Program Process Input? We’ve seen how to use InputStreams to get input from the user from the keyboard (see the EasyReader code) –it waits for the user to type a string and press enter In a GUI-based program, there can be several sources of input; e.g., clicking a button, moving a slider, typing characters in a text field, etc. Invoking methods to wait for user input at specific component won’t work –we can’t predict where the next input will come from

Copyright © 2002, Systems and Computer Engineering, Carleton University Gui1.ppt 18 Event-Driven GUI GUI-based Java programs use the Java Event Model The Java Virtual Machine watches for the user’s actions on components; e.g., –mouse movement/dragging, clicks on components –key presses The JVM creates event objects that correspond to these actions, and sends these event objects to listeners which are provided by the program to handle the events

Copyright © 2002, Systems and Computer Engineering, Carleton University Gui1.ppt 19 Event-Driven GUI java.awt.AWTEvent extends EventObject and is the superclass of all Java 1.1 AWT events Each AWT event is represented by its own subclass; e.g., –java.awt.event.WindowEvent –java.awt.event.MouseEvent Swing adds additional event classes, but uses the Java 1.1 event model We can now design a subclass of JFrame called CloseableFrame that behaves as expected when the Close button is clicked

Copyright © 2002, Systems and Computer Engineering, Carleton University Gui1.ppt 20 Class diagram

Copyright © 2002, Systems and Computer Engineering, Carleton University Gui1.ppt 21 Example - A Closeable Frame import javax.swing.*; import java.awt.event.*; public class CloseableFrame extends JFrame implements WindowListener {

Copyright © 2002, Systems and Computer Engineering, Carleton University Gui1.ppt 22 Example - A Closeable Frame public CloseableFrame() { super(); addWindowListener(this); } public CloseableFrame(String str) { super(str); addWindowListener(this); }

Copyright © 2002, Systems and Computer Engineering, Carleton University Gui1.ppt 23 Example - A Closeable Frame public void windowClosed(WindowEvent event) {} public void windowDeiconified(WindowEvent event) {} public void windowIconified(WindowEvent event) {}

Copyright © 2002, Systems and Computer Engineering, Carleton University Gui1.ppt 24 Example - A Closeable Frame public void windowActivated(WindowEvent event) {} public void windowDeactivated(WindowEvent event) {} public void windowOpened(WindowEvent event) {}

Copyright © 2002, Systems and Computer Engineering, Carleton University Gui1.ppt 25 Example - A Closeable Frame public void windowClosing(WindowEvent event) { // dispose of the JFrame object dispose(); // terminate the program System.exit(0); }

Copyright © 2002, Systems and Computer Engineering, Carleton University Gui1.ppt 26 Example - A Closeable Frame public static void main(String args[]) { JFrame f = new CloseableFrame ("Closeable Frame"); f.setSize(400, 300); f.show(); }

Copyright © 2002, Systems and Computer Engineering, Carleton University Gui1.ppt 27 CloseableFrame Constructors JFrame has two useful constructors: –JFrame() creates an untitled JFrame object –JFrame(String title) creates a JFrame object with the specified title Our CloseableFrame class provides similar constructors

Copyright © 2002, Systems and Computer Engineering, Carleton University Gui1.ppt 28 CloseableFrame When a window is opened, closing, closed, activated, deactivated, iconified, or deiconified, it sends a WindowEvent object to its WindowListener object CloseableFrame is-a JFrame is-a Frame is-a Window, so a CloseableFrame object will send WindowEvents When the Close button is clicked, it notifies the listener object by invoking the object’s windowClosing() method

Copyright © 2002, Systems and Computer Engineering, Carleton University Gui1.ppt 29 Closing the Window We could create a separate class for the listener object; but instead, the CloseableFrame object serves as its own listener for WindowEvent events –CloseableFrame implements the WindowListener interface –the frame registers itself as its own listener by sending itself the addWindowListener(this) message ( addWindowListener() inherited from Window)

Copyright © 2002, Systems and Computer Engineering, Carleton University Gui1.ppt 30 CloseableFrame We want to terminate the application when the CloseableFrame object is closing, so we are interested in the windowClosing() method –CloseableFrame must implement the other 6 methods listed in the WindowListener interface, but they can have empty bodies When the window’s Close button is pushed, it invokes the windowClosing() method of its listener; i.e., the windowClosing() method in class CloseableFrame

Copyright © 2002, Systems and Computer Engineering, Carleton University Gui1.ppt 31 Structure of a JFrame object Title JFrame JRootPane JLayeredPane optional menu bar content pane glass pane Adapted from Core Java 1.2, Volume 1 - Fundamentals, Horstmann & Cornell

Copyright © 2002, Systems and Computer Engineering, Carleton University Gui1.ppt 32 JFrame Methods for manipulating these parts public Container getContentPane() public Component getGlassPane() public JMenuBar getJMenuBar() public JLayeredPane getLayeredPane() public JRootPane getRootPane() public void setContentPane(…) public void setGlassPane(…) public void setJMenuBar(…) public void setLayeredPane(…) public void setRootPane(…)

Copyright © 2002, Systems and Computer Engineering, Carleton University Gui1.ppt 33 Example : Adding a menu to a JFrame Title File Edit Search View Undo Redo Cut Copy Paste JMenuItem JMenu JMenuBar

Copyright © 2002, Systems and Computer Engineering, Carleton University Gui1.ppt 34 What components make up a Menu Bar ? JMenuItem getComponent():Component JMenu +add(i:JMenuItem)JMenuItem JMenuBar +add(m:JMenu):JMenu Container JComponentAbstractButton

Copyright © 2002, Systems and Computer Engineering, Carleton University Gui1.ppt 35 What inputs come from a menu bar ? In response to a user clicking on a JMenuItem object, the Java runtime will generate a java.awt.event.ActionEvent object In order to respond to the ActionEvent, an object must implement java.awt.event.ActionListener that has a single method : public void actionPerformed(ActionEvent e); EventObject +getSource():Object AWTEvent ActionEvent +getActionCommand():String

Copyright © 2002, Systems and Computer Engineering, Carleton University Gui1.ppt 36 Adding a menu to a JFrame public class MenuFrame extends JFrame implements ActionListener { public MenuFrame() { // Set up frame itself – title,size,location JMenuBar menuBar = new JMenuBar( ); setJMenuBar( menuBar ); JMenu fileMenu = new JMenu( "File" ); menuBar.add( fileMenu ); JMenu editMenu = new JMenu( "Edit" ); menuBar.add( editMenu ); JMenu searchMenu = new JMenu( "Search" ); menuBar.add( searchMenu ); JMenu viewMenu = new JMenu( "View" ); menuBar.add( viewMenu );

Copyright © 2002, Systems and Computer Engineering, Carleton University Gui1.ppt 37 Adding a menu to a JFrame JMenuItem item; item = new JMenuItem ( "Undo" ); item.addActionListener( this ); editMenu.add( item ); item = new JMenuItem ( "Redo" ); item.addActionListener( this ); editMenu.add( item ); … // etc for Cut, Copy, Paste

Copyright © 2002, Systems and Computer Engineering, Carleton University Gui1.ppt 38 Adding a menu to a JFrame public void actionPerformed( ActionEvent e ) { System.out.println ( e.getSource() ); System.out.println ( e.getActionCommand() ); } javax.swing.JMenuItem[…] Undo If you select Edit->Undo :