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.

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 Event Listeners Some Events and Their Associated Event Listeners Act that Results in the EventListener Type User clicks a button, presses Enter while.
Web Design & Development Lecture 19. Java Graphics 2.
Programming in Java; Instructor:John Punin Graphics and Graphical User Interfaces1 Programming in Java Graphics and Graphical User Interfaces.
Examples. // A simple Frame with Rectangle Inside import java.awt.*; import javax.swing.*; import java.awt.geom.*; // For Shapes class rectComponent extends.
 Specifies a set of methods (i.e., method headings) that any class that implements that interface must have.  An interface is a type (but is not a class).
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.
CS18000: Problem Solving and Object-Oriented Programming.
Graphic User Interfaces Layout Managers Event Handling.
Bar Graph Design. Left-side/Right-side mechanical processing creative, abstract reasoning.
TCU CoSc Programming with Java Handling Events.
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.
Jan Event Handling -1.1 Yangjun Chen Dept. Business Computing University of Winnipeg.
Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
Event Handling Events and Listeners Timers and Animation.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 15 Event-Driven Programming.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 14 Event-Driven Programming.
Event-Driven Programming
Event Handling. In this class we will cover: Keyboard Events Mouse Events Focus Events Action Interface Multicasting.
© The McGraw-Hill Companies, 2006 Chapter 10 Graphics and event- driven programs.
Lecture 18 Review the difference between abstract classes and interfaces The Cloneable interface Shallow and deep copies The ActionListener interface,
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 16 Event-Driven Programming.
1 Event-Driven Programming Just like designing GUIs, you also will probably not write Java code like the program examples given in these notes. You will.
CSE 331 Software Design & Implementation Dan Grossman Spring 2015 GUI Event-Driven Programming (Based on slides by Mike Ernst, Dan Grossman, David Notkin,
Introduction to GUI Java offers a great number of pre-defined classes to support the development of graphical user interfaces –These are broken down into.
ACM/JETT Workshop - August 4-5, ExceptionHandling and User Interfaces (Event Delegation, Inner classes) using Swing.
GUI Programming in Java: Event Handling & More Components Corresponds with Chapter 14, Chapter 15.
1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers.
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,
Java Programming: Guided Learning with Early Objects
GUI Components and Design Here we add one more component to our programs, JButtons –JButtons can only be inserted into JPanels (or JApplets) –Clicking.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 14 Event-Driven Programming.
(c) University of Washington07b-1 CSC 143 Java Events, Event Handlers, and Threads Reading: Ch. 17.
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.
– 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.
Graphics and Event-Driven Programming in Java John C. Ramirez Department of Computer Science University of Pittsburgh.
MSc Workshop - © S. Kamin, U.Reddy Lect 4 - Events - 1 Lecture 4 – Event Handling r Painting r Event types r Catching different event types.
Omer Boyaci.  GUIs are event driven.  When the user interacts with a GUI component, the interaction—known as an event—drives the program to perform.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 21.1 Test-Driving the Painter Application.
CS-1020 Dr. Mark L. Hornick 1 Event-Driven Programming.
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.
Object Oriented Programming.  Interface  Event Handling.
CSE 331 Software Design & Implementation Hal Perkins Autumn 2012 Event-Driven Programming 1.
Agenda Introduction. Event Model. Creating GUI Application. Event Examples.
Index Event Handling Events Event Source Event Listener Event Classes Action Event Class Adjustment event Class Event Source Event Listener Interface Using.
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,
Event-Driven Programming F Procedural programming is executed in procedural order. F In event-driven programming, code is executed upon activation of events.
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.
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.
Jozef Goetz Credits: Copyright  Pearson Education, Inc. All rights reserved. expanded by J. Goetz, 2016.
Sep 181 Example Program DemoTranslateEnglishGUI.java.
5-1 GUIs and Events Rick Mercer. 5-2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces to respond.
TENTH LECTURE Event and listener. Events and Listeners An event can be defined as a type of signal to the program that something has happened. The event.
Prepared by: Dr. Abdallah Mohamed, AOU-KW Unit7: Event-driven programming 1.
GUI Programming using Java - Event Handling
Chapter 14 Event-Driven Programming
CompSci 230 S Programming Techniques
CHAPTER Reacting to the user.
Chapter 12 Event-Driven Programming
CSE 331 Software Design and Implementation
Programming in Java Event Handling
CSE 331 Software Design and Implementation
Chapter 16 Event-Driven Programming
Events, Event Handlers, and Threads
Presentation transcript:

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 to generate an event We now need to implement event handlers – Event handlers are like exception handlers in that we implement the code to determine how the event is handled – We implement an event handler by implementing an event listener, which is an interface class So now we put the pieces together from the various concepts we learned earlier – Extend JFrame and/or JPanel to place our GUI components onto a visible container – Implement Event Listener interface class(es) to handle the GUI component interaction

Delegation Model The idea is that an action triggers an Event – The Event is listened for by a Listener – We have to add the Listener to the GUI component that can generate the Event – Upon generating an Event, the Listener invokes a proper event handler method which is implemented in your code somewhere

Event Classes Most Event classes are defined in the awt package under java.awt.event We are primarily interested in four types of events, those generated by – a GUI component (JButton, JSlider, JTextBox, etc) – the mouse being moved or the mouse button being used – the keyboard being used – a timer

JButton Handling Since we’ve already looked at JButtons, we start with those – A JButton, when clicked, generates an ActionEvent – The ActionEvent is handled by an ActionListener an interface class, so we must implement the abstract method(s) of that class – The ActionListener defines one abstract method, actionPerformed we must then implement this one method to implement ActionListener – We have to attach to our JButton(s) the ActionListener that implements the actionPerformed method This requires 3 steps 1.after instantiating a JButton, add the instruction button.addActionListener(object); where object is the object that will handle the ActionEvent (usually use this ) 2.add implements ActionListener to whatever class will handle the event 3.implement the actionPerformed method in the class that implements ActionListener –public void actionPerformed(ActionEvent ev) {…}

Simple Example import java.awt.*; import java.awt.event.*; import javax.swing.*; public class SimpleButtonExample { public static void main(String[] args) { // code omitted } public static class ExampleButton extends JPanel implements ActionListener { private JLabel output; private int counter; public ExampleButton() { counter=0; output=new JLabel(""+counter); JButton jb=new JButton("click me"); jb.addActionListener(this); setLayout(new GridLayout(2,1)); add(jb); add(output); } public void actionPerformed(ActionEvent e){ counter++; if(counter>100) counter=0; output.setText(""+counter); }

Multiple JButtons In our previous example, there is only one JButton so there is only one way that actionPerformed is called What if we had multiple JButtons? – Let’s enhance the program to have 3 JButtons, an increment button, a decrement button, and a clear button (to reset counter to 0) – we will name these b1, b2, b3 – We add ActionListener(this) to all three buttons – When any button is clicked, actionPerformed is invoked – How do we determine which button caused the event in actionPerformed in order to know what action to take? – The ActionEvent comes with information such as who caused it we can test this by comparing e.getSource( ) to each of the JButtons using e.getSource()==jb or by comparing the String placed into the JButton using e.getActionCommand( ) as in e.getActionCommand.equals(“click me”)

public static class ExampleButton extends JPanel implements ActionListener { private JLabel output; private JButton b1, b2, b3;// notice we moved the JButton private int counter;// declaration here so that public ExampleButton() {// they are within the scope of counter=0;// actionPerformed output=new JLabel(""+counter); b1=new JButton("increment"); b2=new JButton("decrement"); b3=new JButton("reset"); b1.addActionListener(this); b2.addActionListener(this); b3.addActionListener(this); JPanel p1=new JPanel(); p1.add(b1);p1.add(b2);p1.add(b3); setLayout(new GridLayout(2,1)); add(p1); add(output); } public void actionPerformed(ActionEvent e){ if(e.getSource()==b1) counter++; else if(e.getSource()==b2) counter--; else if(e.getSource()==b3) counter=0; output.setText(""+counter); }

Another Approach We can define new classes, one for each ActionListener –b1.addActionListener(new IncrementActionListener()); –b2.addActionListener(new DecrementActionListener()); –b3.addActionListener(new ResetActionListener()); – Each of these classes need to be defined but would consist solely of a class header which includes implements ActionListener and an actionPerformed method public class IncrementActionListener implements ActionListener { public void actionPerformed(ActionEvent e) { if(e.getSource()==b1) counter++; output.setText(“”+counter); }

Adding JTextField Input Recall from chapter 12 that we saw a GUI that had a JTextField for input and a JLabel for output We used 3 JButtons to control the GUI – Click on Activate and we take what’s in the JTextField and move it to the JLabel – Click on Clear and both fields are reset to blank – Click on Quit and the program exits To obtain the information in a JTextField use getText as in jtf.getText( ) – We then take this text and put it in the label as in lab.setText(jtf.getText( )) – We will do this in actionPerformed if the Activate JButton is clicked

public static class GuiPanel extends JPanel implements ActionListener { private JButton activate, clear, quit; private JTextField jtf; private JLabel lab; public GuiPanel() { // as before (see GUI1.java) except we add: activate.addActionListener(this); clear.addActionListener(this); quit.addActionListener(this); } public void actionPerformed(ActionEvent e){ if(e.getSource()==activate) { lab.setText(jtf.getText()); jtf.setText(""); } else if(e.getSource()==clear) { lab.setText(""); jtf.setText(""); } else if(e.getSource()==quit) System.exit(0); }

Event Types and Listeners ActionSource Event Type ObjectGenerated Click a button JButtonActionEvent Click a check box JCheckBoxItemEvent, ActionEvent Click a radio button JRadioButtonItemEvent, ActionEvent Press return on a text field JTextFieldActionEvent Select a new item JComboBoxItemEvent, ActionEvent Window opened, closed, etc. WindowWindowEvent Mouse pressed, released, etc. ComponentMouseEvent Key released, pressed, etc. ComponentKeyEvent Time limit elapses TimerActionEvent EventListenerMethod ActionEventActionListeneractionPerformed ItemEventItemListeneritemStateChanged MouseEventMouseListener5 methods MouseMotionListenermouseMoved, mouseDragged KeyEventKeyListener3 methods

Comments All of the events (and most listeners) are in java.awt.event – ListSelectionEvent and ChangeEvent are in javax.swing.event The Event types have their own methods which give you access to useful data about the event – ActionEvent has getActionCommand, getSource, getModifiers, getWhen (the timestamp of when the action occurred as a number of milliseconds since Jan 1, 1970) – MouseEvent has methods getX( ), getY( ) and getPoint to obtain the x, y coordinate or the Point of the mouse when the event occurred. getButton to return which mouse button was used, getClickCount (number of clicks associated with this event) – KeyEvent has getKeyChar( ) to obtain the character of the key pressed (e.g., ‘a’ or ‘A’) and getKeyCode( ) which returns an int that can be matched against any of the constants defined in the KeyEvent class such as KeyEvent.VK_UP (up arrow) or KeyEvent.VK_HOME You can implement any number of interfaces in your class – But be aware that some interfaces may interfere with each other if they have overlapping constants or methods!

Example import java.awt.*; import java.awt.event.*; import javax.swing.*; public class SimpleMouseExample { public static void main(String[] args) { // code omitted } public static class MousePanel extends JPanel implements ActionListener, MouseListener { private int x1, y1, x2, y2; private boolean clear; public MousePanel() { x1=x2=y1=y2=-1; JButton reset=new JButton("Reset Drawing"); reset.addActionListener(this); add(reset); addMouseListener(this); clear=false; } public void actionPerformed(ActionEvent e){ clear=true; repaint(); }

public void mouseClicked(MouseEvent e) {} public void mouseEntered(MouseEvent e) {} public void mouseExited(MouseEvent e) {} public void mousePressed(MouseEvent e) { x1=e.getX(); y1=e.getY(); } public void mouseReleased(MouseEvent e) { x2=e.getX(); y2=e.getY(); repaint(); } public void paintComponent(Graphics g){ if(clear){ clear=false; super.paintComponent(g); } else g.drawLine(x1,y1,x2,y2); } } // end inner class } // end outer class

Inner Classes We’ve already seen inner classes in that we define a JPanel subclass inside our JFrame class – Also known as nested classes We can implement our Listeners by indicating that the Listener is handled in this class or by defining another class and implementing it there Typically, implementing the Listener class separately will involve implementing that Listener class as an inner class – The main reason for doing it this way is that no one else will use this class so there is no point in making it a standalone class – NOTE: if the outer class creates a variable of a type of inner class and the outer class has static members, then the inner class must be declared as a static class (see previous example) The inner class is given the name OuterClass$InnerClass as in SimpleMouseExample$MousePanel Alternatively, we can also define an anonymous inner class (as shown in a few slides)

Example

Comments An outer class can contain any number of inner classes An inner class can reference instance data and methods of the outer class An inner class can be defined static but a static inner class can not access non-static outer class members Typically an inner class will be given the visibility modifier of private so that no other classes other than the outer class can make use of it, however this does not have to be the case (see below) Objects of an inner class can be created not only by the outer class but by other classes but if the inner class is not static, you must create an object of the outer class first before other classes can create instances of the inner class

Anonymous Inner Classes Previously we saw that we could add an action listener to a button either using this or new SomeClassWhichImplementsActionListener –button.addActionListener(this); this class must implementActionListener and contain an actionPerformed method –button.addActionListener(new SomeClassWhichImplementsActionListener() ); SomeClassWhichImplementsActionListener must be implemented somewhere –button.addActionListener(new ActionListener( ) { public actionPerformed(ActionEvent) {…} }); – Notice the inner class is anonymous (unnamed) and all we specify is the name of the superclass (which is an interface class) followed by the needed internal portions of this class which in our case is just the actionPerformed method

Comments An anonymous inner class must always extend a superclass or implement an interface Since we are instantiating this inner class into an object, it must implement all abstract methods of the superclass/interface The inner class can only use the parent class’ no- parameter constructor The inner class is still given a name by the compiler, in this case OuterClass$n where n is a number indicating which inner class this is – if there are two in this outer class, then n will be 1 for the first, 2 for the second Is there any value to using anonymous inner classes? – only as a matter of convenience – less typing

Mouse Events and Listeners Mouse interaction generates MouseEvents which are handled by MouseListeners and MouseMotionListeners – We use the instructions addMouseListener(listener); and addMouseMotionListener(listener); where listener is another class (possibly an inner class), an anonymous class or the word this there are 5 methods to implement for MouseListener: mousePressed, mouseReleased, mouseClicked, mouseEntered and mouseExited there are 2 methods to implement for MouseMotionListener: mouseDragged and mouseMoved – Aside from getX, getY, getButton, getClickCount and getWhen, MouseEvent has these other methods: getPoint – returns a Point (x, y coordinate pair) isAltDown, isCountrolDown, isMetaDown, isShiftDown - booleans

KeyEvent and KeyListener Keyboard interaction generates KeyEvents which are handled by KeyListeners – Use addKeyListener(listener); – You must set the JFrame or JPanel to be focusable frame.setFocusable(true); or keypanel.setFocusable(true); – There are three methods to implement: keyPressed, keyReleased and keyTyped – KeyEvent has two methods as we already mentioned, getKeyChar( ) and getKeyCode( ) You would use getKeyChar if you want to test the event’s cause against one of the characters on the keyboard like ‘a’, ‘A’, ‘1’, ‘!’ or ‘ ’ (space) You would use getKeyCode to test for a special key (arrow keys, enter key, etc) The space bar can be tested using getKeyChar as ‘ ’ or getKeyCode as KeyEvent.VK_SPACE (see table 16.3, page 622 for full list of key codes)

Timer and ActionEvents Java has two Timer classes – the older java.util.Timer and the newer javax.swing.Timer – we will look at the newer one The Timer will generate ActionEvents at regular intervals based on a delay that you set – you implement actionPerformed to handle these events, for instance by moving items in a Graphics area to represent animation To use the Timer: –Timer t = new Timer(delay, listener); – delay is the time in milliseconds between ActionEvents being generated – listener will be the class that implements the ActionListener (a nested inner class, an anonymous class, an external class, or this ) – Implement actionPerformed (as with JButtons) If you have both a Timer and JButtons, test to see which caused the event using if(e.getSource( )==t) – Start and stop the Timer using t.start( ); and t.stop( ); – Reset the Timer’s delay using t.setDelay(delay);

private static class TimerPanel extends JPanel { private JButton startstop, reset; private int counter; private Timer t; private boolean startstoptoggle; private JLabel output; public TimerPanel() { startstoptoggle=true; output=new JLabel("Time: " + counter/100); startstop=new JButton("Start"); reset=new JButton("Reset"); startstop.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) {if(startstoptoggle){startstoptoggle=false; t.start();startstop.setText("Stop");} else {startstoptoggle=true;t.stop(); startstop.setText("Start");}}}); reset.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) {counter=0; output.setText ("Time: " + counter/60);}}); t=new Timer(10, new ActionListener() { public void actionPerformed(ActionEvent e) {counter++;output.setText("Time: " + counter/60);}}); JPanel p1=new JPanel(); p1.add(startstop);p1.add(reset); setLayout(new BorderLayout()); add(p1,BorderLayout.NORTH); add(output,BorderLayout.CENTER); } Example omitting the outer class Notice the use of anonymous inner classes

Listener Interface Adapters Another type of class is a listener interface adapter – Such a class provides default implementations for all of the given interface’s methods – For instance, a KeyAdapter implements the three methods needed for a KeyListener These implemented methods are all empty ({ }) and so the only reason to use one is that it saves you from having to implement all of the methods yourself to avoid syntax errors – You would then override only the method or methods that you need in your program – Note that we MouseAdapter for MouseListener, MouseMotionAdapter for MouseMotionListener, KeyAdapter for KeyListener, and WindowAdapter for WindowListener why not ActionAdapter for ActionListener?