Presentation is loading. Please wait.

Presentation is loading. Please wait.

GUI Programming III: Events

Similar presentations


Presentation on theme: "GUI Programming III: Events"— Presentation transcript:

1 GUI Programming III: Events
24 GUI Programming III: Events

2 Header with column labels
Previously Hierarchy Overview Swing Components Swing Components Would you like to save the data? Question! Each cell displays a data item Each column displays one type of data Header with column labels Application title

3 Overview Delegation Event Model Event Object Event Listener
Event Control Flow Registration Event Listener Interfaces Listener Methods Process Action Event Example

4 Delegation Event Model
Who started the action is not who execute it The information is transmitted using an event object The model consisted of the following actors User that start the action GUI component generate an object, e.g. JButton Listener Is notified of the request; receive an event object Execute request; contains the business logic, e.g. Save the data

5 Event Object Created when an event occurs, e.g. user interacts with a GUI component Composed of all necessary information about the When the event has occurred Type of event that has occurred, e.g. keyboard events, mouse event, ... Source of the event, e.g. JButton Of type Event class from java.awt package

6 Event Listener Need to implement the corresponding event listener interface, e.g. ActionListener Need to register with the source object Wait until an valid event is fired Notified of an event of the type of interest The corresponding method in the implemented interface is called with the generated event Processes the received event

7 Event Control Flow Event Source Event Listener Register Fire event
Process event

8 Registration Call following method on the source object void add<Type>Listener(<Type>Listener listenerObj) <Type> refers to the type of event source Can be Key, Mouse, Focus, Component, Action and others One event source can register several listeners Registered listener can unregister by calling the following method on the source object void remove<Type>Listener(<Type>Listener listenerObj)

9 Event Classes EventObject class is part of the java.util package
Subclasses follow the naming convention: <Type>Event Base one already defined in package java.awt.event

10 Event Classes ComponentEvent - Created when the source is moved, resized, made visible or hidden ActionEvent - Created when the source is moved, resized, made visible or hidden ItemEvent - Created when the source is pressed, double clicked or selected, i.e. A button KeyEvent - Created when an item is selected or deselected, e.g. in a list or check box MouseEvent - Created when the mouse is pressed, released, clicked (press and release), on tope or leaving the top of the source WindowEvent - Created when the source (a window) is open, close, activated, deactivated, iconified, ... Just some of the events

11 Event Listener Interfaces
ActionListener must be implement to receive action events (ActionEvent) MouseListener must be implement to receive mouse events (MouseEvent) MouseMotionListener must be implement to receive mouse motion events that include dragging WindowListener must be implement to receive window events (WindowEvent) Defined in package java.awt.event

12 Listener Methods ActionListener void actionPerformed(ActionEvent evt)
Invoked when an action occurs MouseMotionListener void mouseDragged(MouseEvent evt) Invoked when a mouse button is pressed on a component and then dragged void mouseMoved(MouseEvent evt) Invoked when the mouse cursor has been moved onto a component but no buttons have been pushed

13 Listener Methods MuoseListener void mouseClicked(MouseEvent evt)
Invoked when the mouse button has been clicked (pressed and released) on a component void mouseEntered(MouseEvent evt) Invoked when the mouse enters a component void mouseExited(MouseEvent evt) Invoked when the mouse exits a component continue ...

14 Listener Methods MuoseListener (continue)
void mousePressed(MouseEvent evt) Invoked when a mouse button has been pressed on a component  void mouseReleased(MouseEvent evt) Invoked when a mouse button has been released on a component. WindowListener  void windowClose(WindowEvent evt) Invoked when a window has been closed as the result of calling dispose on the window. Many more methods.

15 Process Create Event Listener class; class which implements the appropriate listener interface Implement all methods of the appropriate listener interface Describe in each method how you would like the event to be handled May give empty implementations for methods you don't need

16 Process public class MyActionListener implements ActionListener {
/** * Processes the action event from the "OK" JButton. evt the fired action event. */ public void actionPerformed(ActionEvent evt) { String strAction = evt.getActionCommand(); if (strAction.equals("OK")) { // TODO: add code for when button is pressed } } // actionPerformed() } // end class MyActionListener

17 Process Register the listener object with the event source
The object is an instantiation of the listener class from previous step Use the add<Type>Listener method of the event source MyActionListener oListener = new MyActionListener(); JButton jb = new JButton("OK"); jb.setActionCommand("OK"); jb.addActionListener(oListener); // register the listener ...

18 super("Pressing Buttons"); // Default Layout Manager is FlowLayout
/** * GUI application with two button than when clicked print a message into * the standard out device, i.e. console. */ public class ButtonsGUI extends JFrame implements ActionListener { * Builds the GUI for the application. public ButtonsGUI() { super("Pressing Buttons"); JPanel contentPane= new JPanel(); // Default Layout Manager is FlowLayout JButton jb1 = new JButton("Press me"); JButton jb2 = new JButton("Or press me"); jb1.setActionCommand("Press me"); jb1.addActionListener(this); contentPane.add(jb1); jb2.setActionCommand("Or press me"); jb2.addActionListener(this); contentPane.add(jb2); setContentPane(contentPane); setDefaultCloseOperation(EXIT_ON_CLOSE); pack(); setVisible(true); } // Constructor () * Processes clicking any of the buttons. evt the event on the button. public void actionPerformed(ActionEvent evt) { String strAction = evt.getActionCommand(); if (strAction.equals("Press me")) { System.out.println("'Press me' button was pressed"); } else if (strAction.equals("Or press me")) { System.out.println("'Or press me' button was pressed"); } } // actionPerformed() } // end class ButtonsGUI Action Event Example public ButtonsGUI() { super("Pressing Buttons"); // Default Layout Manager is FlowLayout JPanel contentPane= new JPanel(); // Build buttons JButton jb1 = new JButton("Press me"); JButton jb2 = new JButton("Or press me"); jb1.setActionCommand("Press me"); jb1.addActionListener(this); contentPane.add(jb1); jb2.setActionCommand("Or press me"); jb2.addActionListener(this); contentPane.add(jb2); setContentPane(contentPane); setDefaultCloseOperation(EXIT_ON_CLOSE); pack(); setVisible(true); } // Constructor () public void actionPerformed(ActionEvent evt) { String strAction = evt.getActionCommand(); if (strAction.equals("Press me")) { System.out.println("'Press me' button was pressed"); } else if (strAction.equals("Or press me")) { System.out.println("'Or press me' button was pressed"); } } // actionPerformed() Amadeo Ascó, Adam Moore

19 Action Event Example After pressing both buttons; ‘Press me’ first and ‘Or press me’ the output is as bellow 'Press me' button was pressed 'Or press me' button was pressed


Download ppt "GUI Programming III: Events"

Similar presentations


Ads by Google