Programming in Java; Instructor:John Punin Graphics and Graphical User Interfaces1 Programming in Java Graphics and Graphical User Interfaces.

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

1 Graphical User Interface (GUI) Applications Abstract Windowing Toolkit (AWT) Events Handling Applets.
Introduction to Java 2 Programming
Java GUI building with the AWT. AWT (Abstract Window Toolkit) Present in all Java implementations Described in (almost) every Java textbook Adequate for.
Made with love, by Zachary Langley Applets The Graphics Presentation.
Mari Göransson - KaU - Datavetenskap - DAVD11 1 Java Event Handling --
Mouse Listeners We continue our examination of GUIs by looking at how to interact with the mouse –Just as Java creates Events when the user interacts with.
Graphic User Interfaces Layout Managers Event Handling.
TCU CoSc Programming with Java Handling Events.
Event-Driven Programming You might have realized in creating a GUI and clicking on the JButtons that nothing happens Clicking on a JButton causes the JVM.
Dale Roberts GUI Programming using Java - Mouse Events Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer.
Jan Event Handling -1.1 Yangjun Chen Dept. Business Computing University of Winnipeg.
CS221 © 2007 Ray S. Babcock Menus, Toolbars, and a Mouse Appendix C.6-C.7.
Event Handling. In this class we will cover: Basics of event handling The AWT event hierarchy Semantic and low-level events in the AWT.
Event Handling Events and Listeners Timers and Animation.
CS3157 Java UI Recitation. Material Covered: Overview of AWT components, Event Handling, creating applets, and sample UI. Not covered in recitation: Drawing,
Lecture 18 Review the difference between abstract classes and interfaces The Cloneable interface Shallow and deep copies The ActionListener interface,
EVENTS CSC 171 FALL 2004 LECTURE 16. “Traditional” Input In console applications, user input is under control of the program The program asks the user.
10.1 AWT The AWT classes Users today expect a Graphical User Interface (GUI) Improves application usability Difficult to implement cross-platform.
Events in Java Swing Chris North cs3724: HCI. Typical command line program Non-interactive Linear execution program: main() { code; }
GUI Programming in Java
Java Programming, 3e Concepts and Techniques Chapter 3 Section 65 – Manipulating Data Using Methods – Java Applet.
1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers.
Chapter 11 Java AWT Part I: Mouse Events (Optional) Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin,
Java GUIs and Graphics CNS Outline  Introduction  Events  Components  Layout managers  Drawing  Introduction  Events  Components  Layout.
1 Event Driven Programs Rick Mercer. 2 So what happens next?  You can layout a real pretty GUI  You can click on buttons, enter text into a text field,
More Event Handling Adapters Anonymous Listeners Pop menus Validating User Input.
Java GUI’s are event driven, meaning they generate events when the user interacts with the program. Typical events are moving the mouse, clicking a mouse.
Session 11 Border Layout, using Panels, Introduction to PinBallGame.
Dale Roberts GUI Programming using Java - Event Handling Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer.
CSE 501N Fall ‘09 20: Event Handling and Inner Classes 17 November 2009 Nick Leidenfrost.
7/3/00SEM107- © Kamin & ReddyClass 11 - Events - 1 Class 11 - Events r A couple of odds & ends m Component sizes  switch statement r Event types r Catching.
Graphical User Interface Components: Part 1 Chapter 11.
 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.
MSc Workshop - © S. Kamin, U.Reddy Lect 4 - Events - 1 Lecture 4 – Event Handling r Painting r Event types r Catching different event types.
Layout Managers Arranges and lays out the GUI components on a container.
CompSci 230 S Software Construction Applets & AWT.
Pravin Yannawar, DOCS, NMU Jalgaon. Basic Java : Event handling in AWT and Swing 2 Objectives of This Session Explain the Event handling mechanism & demonstrate.
For (int i = 1; i
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 21.1 Test-Driving the Painter Application.
UID – Event Handling and Listeners Boriana Koleva
CS-1020 Dr. Mark L. Hornick 1 Event-Driven Programming.
The Abstract Window Toolkit (AWT) supports Graphical User Interface (GUI) programming. AWT features include: a rich set of user interface components; a.
CS1054: Lecture 21 - Graphical User Interface. Graphical User Interfaces vs. Text User Interface.
Universidad Nacional de Colombia Facultad de Ingeniería Departamento de Sistemas ertificación en AVA.
CMSC 341 Making Java GUIs Functional. 09/29/2007 CMSC 341 Events 2 More on Swing Great Swing demo at /demos/jfc/SwingSet2/SwingSet2Plugin.html.
Week 6: Basic GUI Programming Concepts in Java Example: JFrameDemo.java container : a screen window/applet window/panel that groups and arranges components.
Object Oriented Programming.  Interface  Event Handling.
Agenda Introduction. Event Model. Creating GUI Application. Event Examples.
Event Handling. User actions are called “events” – Low-level window events Window changes – Low-level component events Mouse events, Keyboard events,
1 Event Driven Programs Rick Mercer. 2 So what happens next?  You can layout a real pretty GUI  You can click on buttons, enter text into a text field,
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
What Is an Event? Events – Objects that describe what happened Event sources – The generator of an event Event handlers – A method that receives an event.
Event Handling H_Func(Event) { } Event Receiver Object Source Object Registration.
Mouse Events GUI. Types of Events  Below, are some of the many kinds of events, swing components generate. Act causing EventListener Type User clicks.
Event Handling and Listeners in SWING The practice of event handling.
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
Event Handling CS 21a: Introduction to Computing I First Semester,
Java Programming (By Rama Bhadra Rao M) You can Trace me in Day 5
Prepared by: Dr. Abdallah Mohamed, AOU-KW Unit7: Event-driven programming 1.
Events and Event Handling
Mouse Event Handling in Java (Review)
Advanced User Interfaces
Introduction to Computing Using Java
GUI Programming using Java - Mouse Events
Web Design & Development Lecture 12
Events, Event Handlers, and Threads
Making Java GUIs Functional
Presentation transcript:

Programming in Java; Instructor:John Punin Graphics and Graphical User Interfaces1 Programming in Java Graphics and Graphical User Interfaces

Programming in Java; Instructor:John Punin Graphics and Graphical User Interfaces2 Display of AkmMimic

Programming in Java; Instructor:John Punin Graphics and Graphical User Interfaces3 Classes used in Mimic

Programming in Java; Instructor:John Punin Graphics and Graphical User Interfaces4 Display Components  Basic code to display components import java.applet.Applet; import java.awt.*; public class AkmMimic extends Applet { public void init () { this.add (new TextField(20)); this.add (new Label ("No news is good news")); this.add (new Button ("OK")); }  Applet is a type of Container  Can add components to containers  Container takes care of "laying out the components"  Default behavior for Applets is "Flow Layout" –Left to right, goes to new rows as necessary

Programming in Java; Instructor:John Punin Graphics and Graphical User Interfaces5 Events  Events can occur on Components  There are many types of events –Action, Mouse, Key, Focus, Paint, Window  Listeners watch for events on component  Listeners in Java are interfaces –(must be implemented by a class)  Examples: –ActionListener, MouseListener, MouseMotionListener  Listeners are passed Event objects  Event objects contain information (e.g. x,y position of mouse)  Examples –ActionEvent, MouseEvent, KeyEvent

Programming in Java; Instructor:John Punin Graphics and Graphical User Interfaces6 Events  Different components can generate different events  Button, List, TextField, MenuItem –generate ActionEvents  Any component can generate MouseEvents  To handle events, have to add listeners Button b = new Button ("OK"); ActionListener al = … create an action listener … b.addActionListener (al);  ActionListener is an interface  Need a class that implements the interface public abstract interface ActionListener extends EventListener { public abstract void actionPerformed (ActionEvent e); }

Programming in Java; Instructor:John Punin Graphics and Graphical User Interfaces7 Listening to the OK button import java.applet.Applet; import java.awt.*; import java.awt.event.*; public class AkmMimic extends Applet { public void init () { this.add (new TextField(20)); this.add (new Label ("No news is good news")); Button b = new Button ("OK"); ActionListener al = new OK_Handler(); b.addActionListener (al); this.add (b); } class OK_Handler implements ActionListener { public void actionPerformed (ActionEvent e) { System.out.println ("You pressed OK"); }

Programming in Java; Instructor:John Punin Graphics and Graphical User Interfaces8 Applets can also Listen import java.applet.Applet; import java.awt.*; import java.awt.event.*; public class AkmMimic extends Applet implements ActionListener { public void init () { this.add (new TextField(20)); this.add (new Label ("No news is good news")); Button b = new Button ("OK"); b.addActionListener (this); this.add (b); } public void actionPerformed (ActionEvent e) { System.out.println ("You pressed OK"); }

Programming in Java; Instructor:John Punin Graphics and Graphical User Interfaces9 Copying from TextField to Label import java.applet.Applet; import java.awt.*; import java.awt.event.*; public class AkmMimic extends Applet implements ActionListener { TextField textfield = new TextField(20); Label label = new Label ("No news is good news"); public void init () { this.add (textfield); this.add (label)); Button b = new Button ("OK"); b.addActionListener (this); this.add (b); } public void actionPerformed (ActionEvent e) { label.setText (textfield.getText()); }

Programming in Java; Instructor:John Punin Graphics and Graphical User Interfaces10 Listeners  Some listeners have multiple methods public abstract interface MouseListener extends EventListener { public abstract void mouseClicked(MouseEvent e); public abstract void mouseEntered(MouseEvent e); public abstract void mouseExited(MouseEvent e); public abstract void mousePressed(MouseEvent e); public abstract void mouseReleased(MouseEvent e); };  Classes that implement interfaces  MUST define code for all methods of the interface –If you only need code in one method, solution isn't clean

Programming in Java; Instructor:John Punin Graphics and Graphical User Interfaces11 Handling Mouse Events import java.applet.Applet; import java.awt.*; import java.awt.event.*; public class AkmMimic extends Applet { public void init () { this.add (new TextField(20)); this.add (new Label ("No news is good news")); this.add (new Button("OK")); this.addMouseListener (new MyMouseHandler()); } class MyMouseHandler implements MouseListener { public void mouseClicked(MouseEvent e) { } public void mouseEntered(MouseEvent e) { } public void mouseExited(MouseEvent e) { } public void mousePressed(MouseEvent e) { System.out.println ("You pressed a mouse button"); } public void mouseReleased(MouseEvent e) { } }

Programming in Java; Instructor:John Punin Graphics and Graphical User Interfaces12 Using Adapters to Listen  For most listeners, there is a corresponding adapter class MouseAdapter implements MouseListener { public void mouseClicked(MouseEvent e) { } public void mouseEntered(MouseEvent e) { } public void mouseExited(MouseEvent e) { } public void mousePressed(MouseEvent e) { } public void mouseReleased(MouseEvent e) { } }  You can reuse these, and only implement necessary methods class MyMouseHandler extends MouseAdapter { public void mousePressed (MouseEvent e) { System.out.println ("You pressed a mouse button"); }

Programming in Java; Instructor:John Punin Graphics and Graphical User Interfaces13 Using MouseAdapter mport java.applet.Applet; import java.awt.*; import java.awt.event.*; public class AkmMimic extends Applet { public void init () { this.add (new TextField(20)); this.add (new Label ("No news is good news")); this.add (new Button("OK")); this.addMouseListener (new MyMouseHandler()); } class MyMouseHandler extends MouseAdapter { public void mousePressed(MouseEvent e) { System.out.println ("You pressed a mouse button"); }

Programming in Java; Instructor:John Punin Graphics and Graphical User Interfaces14 Too many classes  A typical GUI has many buttons, components, menu items, etc.  Need a different class for each listener  Too many classes  Confuses name space  Java's solution  Anonymous, inner classes –Class is embedded in another class –Class does not have to have a name provided by developer

Programming in Java; Instructor:John Punin Graphics and Graphical User Interfaces15 Anonymous Classes import java.applet.Applet; import java.awt.*; import java.awt.event.*; public class AkmMimic extends Applet { public void init () { this.add (new TextField(20)); this.add (new Label ("No news is good news")); this.add (new Button("OK")); this.addMouseListener (new MouseAdapter () { public void mousePressed(MouseEvent e) { System.out.println ("You pressed a mouse button"); }}); }

Programming in Java; Instructor:John Punin Graphics and Graphical User Interfaces16 Left, Right, or Middle Button? import java.applet.Applet; import java.awt.*; import java.awt.event.*; public class AkmMimic extends Applet { public void init () { this.add (new TextField(20)); this.add (new Label ("No news is good news")); this.add (new Button("OK")); this.addMouseListener (new MouseAdapter () { public void mousePressed(MouseEvent e) { if (e.isMetaDown()) { System.out.println ("Right mouse button pressed"); } else if (e.isAltDown()) { System.out.println ("Middle mouse button pressed"); } else { System.out.println ("Left mouse button pressed"); } }}); }