Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS-1020 Dr. Mark L. Hornick 1 Event-Driven Programming.

Similar presentations


Presentation on theme: "CS-1020 Dr. Mark L. Hornick 1 Event-Driven Programming."— Presentation transcript:

1 CS-1020 Dr. Mark L. Hornick 1 Event-Driven Programming

2 CS-1020 Dr. Mark L. Hornick 2 Writing GUI applications requires a style of program control called event-driven programming An event occurs when a user (like you) interacts with a GUI object (like a JButton)

3 CS-1020 Dr. Mark L. Hornick 3 Spy++ Demo Spy++ is a utility for snooping on events that are generated within Windows

4 CS-1020 Dr. Mark L. Hornick 4 Event-driven programs… …contain two special types of objects: Event source objects - GUI objects (like JButton) which generate the events When an event is generated, the operating/windowing system captures the generated event Event listener objects Subscribe to the operating/windowing system to be notified when certain events occur

5 CS-1020 Dr. Mark L. Hornick 5 Event Handling When the operating system captures an event, it notifies a subscribed event listener object by calling the event listener’s event handling method If no event listener object is subscribed to listen for an event, the events from the event source object are ignored OS

6 CS-1020 Dr. Mark L. Hornick 6 There are many types of Events One of the most common is an Action Event Some Events and Their Associated Event Listeners Act that Results in the EventEvent Listener Type Action: User clicks a button, presses Enter while typing in a text field, or chooses a menu item ActionListener User closes a frame (main window) WindowListener User presses a mouse button while the cursor is over a component MouseListener User moves the mouse over a component MouseMotionListener Component becomes visible ComponentListener Component gets the keyboard focus FocusListener Table or list selection changes ListSelectionListener Any property in a component changes such as the text on a label PropertyChangeListener

7 CS-1020 Dr. Mark L. Hornick 7 Handling Action Events An object that can be registered as an Action Listener (i.e. can subscribe to Action Events) must be an instance of a class that is declared specifically for the purpose That is, it the class must implement the ActionListener interface To subscribe an Action Listener to an Action Event source, the event source’s addActionListener method must be called with a reference to the Action Listener object as its argument. i.e. JButton has an addActionListener() method: JButton btnQuit = new JButton(“Quit”); btnQuit.addActionListener( eventHandler ); // eventHandler is an ActionListener

8 CS-1020 Dr. Mark L. Hornick 8 ActionListener is a Java interface Recall: Any class that implements a Java interface must provide the method body to all the methods defined in the interface The ActionListener interface only defines a single method – actionPerformed()

9 CS-1020 Dr. Mark L. Hornick 9 Which class or classes should be Action Listeners? First alternative: 1. Have a separate class listen for events This separates GUI creation and event handling Could be good if there is a lot of non-GUI logic involved in responding to events

10 CS-1020 Dr. Mark L. Hornick 10 Defining a separate class that implements ActionListener: import java.awt.event.*; class EventHandler implements ActionListener {... public void actionPerformed( ActionEvent evt ){... } The system notifies an Action Listener by calling the listener’s actionPerformed() method The actionPerformed() method is passed a single argument: a reference to an ActionEvent object

11 CS-1020 Dr. Mark L. Hornick 11 We need to handle events from multiple sources… A single Action Listener can be subscribed to multiple event sources (e.g. many buttons) The Action Listener must be able to distinguish one source from another

12 CS-1020 Dr. Mark L. Hornick 12 Determining an event source – method 1: by event object First, we use the Java instanceof operator to determine the class to which the event source belongs: public void actionPerformed( ActionEvent evt ){ if (evt.getSource() instanceof JButton ){ //event source is a button (but which one??)... } else if (evt.getSource() instanceof JTextFrame ) { //event source is a JTextFrame (but which one??)... }

13 CS-1020 Dr. Mark L. Hornick 13 Once we know the Class of the item that generated the event… …we use the getSource() method to return a reference to the Object that generated the event: if( evt.getSource() instanceof JButton ){ JButton btn= (JButton) evt.getSource(); Note that the getSource returns a reference to an object of the class Object An Object reference can refer to any type of class, so we typecast the returned Object reference to a JButton reference in order to be able to access JButton methods.

14 CS-1020 Dr. Mark L. Hornick 14 Identifying the specific event object Once you know that the source was a JButton, you can check the source against your program’s inventory of JButton objects: if (evt.getSource() instanceof JButton ){ //event source is a button (but which one??) JButton btn = (JButton)evt.getSource(); if( btn == btnQuit ) … else if( btn == btnCancel ) … }

15 CS-1020 Dr. Mark L. Hornick 15 Determining an event source – method 2: by action command The event’s getActionCommand() method can be used to get the text of the object’s Action Command that generated the event: String command = evt.getActionCommand(); if(command.equals(“Quit”) // the Quit button... else if( command.equals(“Cancel”) // Cancel button By default, the Action Command text is the same as the text that appears on the button face (for a JButton) or the text with a JTextField So what do you do if the button does not contain text? And a user can enter just about any text into a JTextField

16 CS-1020 Dr. Mark L. Hornick 16 setActionCommand method The setActionCommand() method can be used to set the Action Command text for any component btnQuit = new JButton ("Quit"); btnQuit.setActionCommand(“Close”); textDate = new JTextField(); textDate.setActionCommand(“Begin”); The text of the Action Command is up to you Calling setActionCommand() does not change the text on the button face or within the text field (that’s done with setText() )

17 CS-1020 Dr. Mark L. Hornick 17 Multiple Action Event handlers Likewise, multiple Action Listeners can be subscribed to a single event source. When an event source generates an event, the system notifies all matching registered ActionListeners Using multiple listeners is not very common

18 CS-1020 Dr. Mark L. Hornick 18 Some consequences of implementing a separate class for Action Listeners 1. GUI creation and event handling are separated Could be good if there is a lot of non-GUI logic involved in responding to events 2. We could separate event handling into several classes in order to avoid all the if and instanceOf logic for determining the event source 3. The Action Listener classes can’t access the variables that reference GUI objects unless the objects are public (not a good approach) or public accessor methods are provided to access each UI component (good, but that could lead to a lot of accessors)

19 CS-1020 Dr. Mark L. Hornick 19 Which class or classes should be Action Listeners? Second alternative: Have the class that created the GUI in the first place listen for all events GUI creation and event handling all in a single class But still lots of if statements and instanceOf checks inside the actionPerformed method

20 CS-1020 Dr. Mark L. Hornick 20 UML Sequence Diagram

21 CS-1020 Dr. Mark L. Hornick 21 Which class or classes should be Action Listeners? Third alternative: Use separate inner classes to listen for events GUI creation and event handling separated Could be good if there is a lot of non-GUI logic involved in responding to events Could separate event handling into several classes in order to avoid all the if and instanceOf logic for determining the event source The Action Listener inner classes CAN access the variables that reference GUI objects (keeping the objects private)

22 CS-1020 Dr. Mark L. Hornick 22 What’s an inner class? An inner class is another class defined inside the same java file - inside the enclosing class import java.awt.event.*; class MyGUIClass { // create GUI and implement usual methods here // define an inner class for handling events class AnActionListener implements ActionListener { public void actionPerformed( ActionEvent evt ){ // handle events here } } // end of inner class } // end of enclosing class

23 CS-1020 Dr. Mark L. Hornick 23 Inner class properties The inner class can access all the members (public or private) of the enclosing class The inner class is private to the outside world You can’t create an instance of an inner class – only the enclosing class You can have any number of inner classes

24 CS-1020 Dr. Mark L. Hornick 24 Handling events Multiple Action Listeners can handle multiple related events This varies according to the judgment of the program designers

25 CS-1020 Dr. Mark L. Hornick 25 Performance Issues Event-handling code executes in an single thread, the event-dispatching thread. This ensures that each event handler finishes execution before the next one executes. Painting code also executes in the event- dispatching thread. Therefore, event-handling code should execute quickly so that the program’s GUI stays responsive. If an event takes too long to execute, the GUI will freeze--that is, it won’t repaint or respond to mouse clicks.


Download ppt "CS-1020 Dr. Mark L. Hornick 1 Event-Driven Programming."

Similar presentations


Ads by Google