©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 10: Event Handling 1 Chapter 10 Event Handling.

Slides:



Advertisements
Similar presentations
Multithreading : animation. slide 5.2 Animation Animation shows different objects moving or changing as time progresses. Thread programming is useful.
Advertisements

Managing Input Events in Swing Week 5 Workshop Lyn Bartram.
Computer Science 209 Graphics and GUIs. Working with Color The class java.awt.Color includes constants for typical color values and also supports the.
Financial Engineering Project Course. Week 7 Java GUI programming and Java Threads GUI example taken from “Computing Concepts with Java 2” by Cay Horstmann.
1 Anonymous Classes A local class is a class that is defined in a block. This could be a method body, a constructor, a local block, a static initializer.
Gui Interfaces a la Swing Up to speed with Swing by Steven Gutz is a good source It don’t mean a thing if it ain’t got that swing Duke Ellington.
1 Event Driven Programming with Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
Event-Driven Programming
OOP Java1 Event Handling Overview Listeners, Adapters and Event Sources Inner classes Event Handling Details Applets and GUI Applications Event.
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 lecture 12Lecture 13 Event Handling (cont.) Overview  Handling Window Events.  Event Adapters Revisited.  Introduction to Components and Containers.
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.
Lesson 35: Review of the Java GUI. The JFrame, Container and JButton.
Object-Oriented Programming (Java), Unit 18 Kirk Scott 1.
Object-Oriented Programming (Java), Unit 19 Kirk Scott 1.
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.
Graphical User Interface CSI 1101 N. El Kadri. Plan - agenda Graphical components Model-View-Controller Observer/Observable.
MIT AITI 2003 Lecture 17. Swing - Part II. The Java Event Model Up until now, we have focused on GUI's to present information (with one exception) Up.
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,
Adding Graphics to a Frame Application Applets: Can generate drawings by overriding paint Frame: Do not draw directly on a frame. Draw graphics on a JPanel.
More Event Handling Adapters Anonymous Listeners Pop menus Validating User Input.
Chapter 12 Event Handling. Chapter Goals To understand the Java event model To install action and mouse event listeners To accept input from buttons,
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.
(c) University of Washington07b-1 CSC 143 Java Events, Event Handlers, and Threads Reading: Ch. 17.
CSE 501N Fall ‘09 20: Event Handling and Inner Classes 17 November 2009 Nick Leidenfrost.
Object-Oriented Programming (Java), Unit 19 Kirk Scott 1.
Previous programs used a JLabel for OUTPUT. Another Swing component that can be used for both user input and output is the JTextfield. Suppose we want.
Fall 2006Adapded from Java Concepts Companion Slides1 Event Handling Advanced Programming ICOM 4015 Lecture 13 Reading: Java Concepts Chapter 12.
– Advanced Programming P ROGRAMMING IN Lecture 21 Introduction to Swing.
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
Object-Oriented Programming (Java), Unit 18 Kirk Scott 1.
Chapter 12: Graphical User Interfaces 1 ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e 1 Chapter 12 Graphical User Interfaces JAVA.
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.
Copyright © 2002, Systems and Computer Engineering, Carleton University b-Gui2.ppt * Object-Oriented Software Development Part 18-b Building.
Swing GUI Components You can create graphics components to place on your applet using classes available in the Swing package ( javax.swing ) Class names.
MSc Workshop - © S. Kamin, U.Reddy Lect 4 - Events - 1 Lecture 4 – Event Handling r Painting r Event types r Catching different event types.
COMP 321 Week 2. Outline Event-Driven Programming Events, Event Sources, Event Listeners Button and Timer Events Mouse Events, Adapters.
1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.
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.
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.
Java GUI. Graphical User Interface (GUI) a list a button a text field a label combo box checkbox.
©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 10: Event Handling 1 Event Handling.
GUI DYNAMICS Lecture 11 CS2110 – Fall GUI Statics and GUI Dynamics  Statics: what’s drawn on the screen  Components buttons, labels, lists, sliders,
CHAPTER 10 EVENT HANDLING. CHAPTER GOALS To understand the Java event model To install mouse and action listeners To accept mouse and text input To display.
Mouse Listeners Moving the mouse will also generate events like the Timer –To have your program respond, you must implement either or both of MouseListener.
Event-Driven Programming CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
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.
Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. Event Handling and GUI Components.
Mouse Events GUI. Types of Events  Below, are some of the many kinds of events, swing components generate. Act causing EventListener Type User clicks.
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
Swing GUI Components So far, we have written GUI applications which can ‘ draw ’. These applications are simple, yet typical of all Java GUI applications.
Lesson 28: More on the GUI button, frame and actions.
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.
GUI Tutorial Day 4. More GUI action  adding a Mouse Listener  SimpleDots  Simple mouse listener  Draw an oval where the mouse is clicked  Box example.
5-1 GUIs and Events Rick Mercer. 5-2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces to respond.
Prepared by: Dr. Abdallah Mohamed, AOU-KW Unit7: Event-driven programming 1.
GUI Programming in Java Hao Jiang Boston College April, 2009.
1 Event Driven Programming with Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
Events and Event Handling
CompSci 230 S Programming Techniques
Computer Science 209 Graphics and GUIs.
GUI III IS
Chapter 4 Interface Types and Polymorphism Part 2
Event-driven programming for GUI
Chapter 12 Event Handling
Events, Event Handlers, and Threads
Presentation transcript:

©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 10: Event Handling 1 Chapter 10 Event Handling

©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 10: Event Handling 2 Events Mouse move Key press Button click... originate from event source processed by event listener

©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 10: Event Handling 3 Event classes Event class (e.g. MouseEvent ). Contains information about the event (e.g. mouse location) Event listener. Knows what to do when the event occurs. Implements listener interface (e.g. MouseListener ) Event source. Keeps a list of listeners and notifies them when the event occurs

©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e 4 Chapter 10: Event Handling 4 Figure 1 Event Classes

©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e 5 Chapter 10: Event Handling 5 Listening to mouse events MouseListener interface has 5 methods – void mouseClicked(MouseEvent e) – void mouseEntered(MouseEvent e) – void mouseExited(MouseEvent e) – void mousePressed(MouseEvent e) – void mouseReleased(MouseEvent e)

©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e 6 Chapter 10: Event Handling 6 Spying on events class MouseSpy implements MouseListener { public void mouseClicked(MouseEvent e) { System.out.println ("Mouse clicked. x=" + e.getX() + " y=" + e.getY()); } // same for other 4 listener methods }

©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e 7 Chapter 10: Event Handling 7 Adding the listener public class MouseSpyApplet { public MouseSpyApplet() { MouseSpy listener = new MouseSpy(); addMouseListener(listener); } }

©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e 8 Chapter 10: Event Handling 8 Figure 2 Spying on Mouse Events

©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e 9 Chapter 10: Event Handling 9 Event Adapters Most mouse listeners only need one or two of the 5 listener methods Boring to redefine the others to do nothing MouseAdapter implements all 5 methods to do nothing. Extend MouseAdapter and just override the methods you need Other listener interfaces with > 1 method also have adapters

©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e 10 Chapter 10: Event Handling 10 Move egg to mouse position public class EggApplet extends Applet { public EggApplet() { egg = new Ellipse2D.Double(...); } public void paint(Graphics g) { Graphics2D g2 = (Graphics2D)g; g2.draw(egg); } private Ellipse2D.Double egg; }

©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e 11 Chapter 10: Event Handling 11 Add mouse listener public EggApplet() {... MouseClickListener listener = new MouseClickListener(); addListener(listener); } class MouseClickListener extends MouseAdapter { public void mouseClicked(MouseEvent e) { // set egg center to // e.getX(), e.getY() } }

©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e 12 Chapter 10: Event Handling 12 Make the listener an inner class Listener can't access egg instance variable Make it an inner class class EggApplet {... private class MouseClickListener extends MouseAdapter {... } } Inner class methods can access outer class instance variables

©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e 13 Chapter 10: Event Handling 13 Inner classes class EggApplet extends Applet {... private class MouseClickListener extends MouseAdapter { public void mouseClicked (MouseEvent e) { int mouseX = e.getX(); int mouseY = e.getY(); egg.setFrame(...); repaint(); } } }

©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e 14 Chapter 10: Event Handling 14 Accessing outer class features Inner class has no egg instance variable Accesses outer class variable Inner class has no repaint method Calls outer class method On which EggApplet instance? The one that constructed the inner class object—the inner class object remembers who constructed it

©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e 15 Chapter 10: Event Handling 15 Figure 3 An Inner-Class Object Remembers the Outer-Class Object That Constructed It

©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e 16 Chapter 10: Event Handling 16 Program EggApplet.java import java.applet.Applet; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.awt.geom.Ellipse2D; public class EggApplet extends Applet { public EggApplet() { egg = new Ellipse2D.Double(0, 0, EGG_WIDTH, EGG_HEIGHT); // add mouse click listener MouseClickListener listener = new MouseClickListener(); addMouseListener(listener); }

©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e 17 Chapter 10: Event Handling 17 public void paint(Graphics g) { Graphics2D g2 = (Graphics2D)g; g2.draw(egg); } private Ellipse2D.Double egg; private static final double EGG_WIDTH = 30; private static final double EGG_HEIGHT = 50; // inner class definition private class MouseClickListener extends MouseAdapter { public void mouseClicked(MouseEvent event) { int mouseX = event.getX(); int mouseY = event.getY(); // now move the ellipse to ( mouseX, mouseY )

©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e 18 Chapter 10: Event Handling 18 egg.setFrame(mouseX - EGG_WIDTH / 2, mouseY - EGG_HEIGHT / 2, EGG_WIDTH, EGG_HEIGHT); repaint(); }

©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e 19 Chapter 10: Event Handling 19 Figure 4 Handling Mouse Events

©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e 20 Chapter 10: Event Handling 20 Graphical Applications Live inside frame window Set size and title Call show method That ends the main method, but the program keeps running GUI thread handles events

©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e 21 Chapter 10: Event Handling 21 Figure 5 A Frame Window

©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e 22 Chapter 10: Event Handling 22 Program FrameTest1.java import javax.swing.JFrame; public class FrameTest1 { public static void main(String[] args) { EmptyFrame frame = new EmptyFrame(); frame.setTitle("Frame Test"); frame.show(); } class EmptyFrame extends JFrame { public EmptyFrame() { final int DEFAULT_FRAME_WIDTH = 300; final int DEFAULT_FRAME_HEIGHT = 300; setSize(DEFAULT_FRAME_WIDTH, DEFAULT_FRAME_HEIGHT); }

©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e 23 Chapter 10: Event Handling 23 Quitting Graphical Apps Define window closer: class WindowCloser extends WindowAdapter { public void windowClosing (WindowEvent e) { System.exit(0); } } Add as window listener to the frame

©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e 24 Chapter 10: Event Handling 24 Program FrameTest2.java import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import javax.swing.JFrame; public class FrameTest2 { public static void main(String[] args) { EmptyFrame frame = new EmptyFrame(); frame.setTitle("Close me!"); frame.show(); }

©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e 25 Chapter 10: Event Handling 25 class EmptyFrame extends JFrame { public EmptyFrame() { final int DEFAULT_FRAME_WIDTH = 300; final int DEFAULT_FRAME_HEIGHT = 300; setSize(DEFAULT_FRAME_WIDTH, DEFAULT_FRAME_HEIGHT); WindowCloser listener = new WindowCloser(); addWindowListener(listener); } private class WindowCloser extends WindowAdapter { public void windowClosing(WindowEvent event) { System.exit(0); }

©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e 26 Chapter 10: Event Handling 26 Add GUI components Panels (for drawing) Text fields (for text input) Buttons Menus

©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e 27 Chapter 10: Event Handling 27 Drawing on a Panel public class MyPanel extends JPanel { public void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2 = (Graphics2D)g; // your drawing code here } }

©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e 28 Chapter 10: Event Handling 28 Figure 6 Anatomy of a Swing Frame

©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e 29 Chapter 10: Event Handling 29 Layout Manager Content pane uses the border layout by default Container contentPane = getContentPane(); contentPane.add(myPanel, "Center");

©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e 30 Chapter 10: Event Handling 30 Figure 7 Component Areas of a Border Layout

©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e 31 Chapter 10: Event Handling 31 Text input JTextField textField = new JTextField(); contentPane.add(textField, "South"); JTextField generates action events ActionListener interface has a single method: actionPerformed

©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e 32 Chapter 10: Event Handling 32 Figure 8 A Frame with a Text Field

©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e 33 Chapter 10: Event Handling 33 Processing Text Input class TextFieldListener implements ActionListener { public void actionPerformed (ActionEvent event) { // get user input String input = textField.getText(); // process input... // clear text field input.setText(""); } }

©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e 34 Chapter 10: Event Handling 34 Figure 9 Drawing Random Eggs

©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e 35 Chapter 10: Event Handling 35 Program Eggs.java import java.awt.Container; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.awt.geom.Ellipse2D; import java.util.Random; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JTextField;

©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e 36 Chapter 10: Event Handling 36 public class Eggs { public static void main(String[] args) { EggFrame frame = new EggFrame(); frame.setTitle("Enter number of eggs"); frame.show(); } class EggFrame extends JFrame { public EggFrame() { final int DEFAULT_FRAME_WIDTH = 300; final int DEFAULT_FRAME_HEIGHT = 300; setSize(DEFAULT_FRAME_WIDTH, DEFAULT_FRAME_HEIGHT); addWindowListener(new WindowCloser()); // construct components panel = new EggPanel();

©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e 37 Chapter 10: Event Handling 37 textField = new JTextField(); textField.addActionListener(new TextFieldListener()); // add components to content pane Container contentPane = getContentPane(); contentPane.add(panel, "Center"); contentPane.add(textField, "South"); } private JTextField textField; private EggPanel panel; private class TextFieldListener implements ActionListener { public void actionPerformed(ActionEvent event) { // get user input String input = textField.getText(); // process user input

©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e 38 Chapter 10: Event Handling 38 panel.setEggCount(Integer.parseInt(input)); // clear text field textField.setText(""); } private class WindowCloser extends WindowAdapter { public void windowClosing(WindowEvent event) { System.exit(0); } class EggPanel extends JPanel { public void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2 = (Graphics2D)g;

©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e 39 Chapter 10: Event Handling 39 // draw eggCount ellipses with random centers Random generator = new Random(); for (int i = 0; i < eggCount; i++) { double x = getWidth() * generator.nextDouble(); double y = getHeight() * generator.nextDouble(); Ellipse2D.Double egg = new Ellipse2D.Double(x, y, EGG_WIDTH, EGG_HEIGHT); g2.draw(egg); } /** Sets the number of eggs to be drawn and repaints the count the new number of eggs */

©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e 40 Chapter 10: Event Handling 40 public void setEggCount(int count) { eggCount = count; repaint(); } private int eggCount; private static final double EGG_WIDTH = 30; private static final double EGG_HEIGHT = 50; }