Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 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 with the GUI

2 Some typical component events and listeners Act that results in eventListener User clicks a button, presses return while typing in a text field, or chooses a menu item ActionListener User closes a 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

3 Implementing listeners (1) Three key bits of code 1) add interface 2) register 3) handle Components can have multiple listeners A simple JButton ActionListener…

4 Implementing listeners (2) public class myClass … implements ActionListener { … // where setting up occurs (e.g. constructor) JButton button = new JButton(I am a button); button.addActionListener(this); … public void actionPerformed(ActionEvent e) { …// respond to event } // end response method } // end class

5 Types of event listeners (1) Global component listeners may be used for any Swing components Types ComponentListener (changes in size, position, visibility) FocusListener (whether ability for keyboard input) KeyListener (key press events, only with focus) MouseListener (clicks and movement into/out of component area) MouseMotionListener (changes in position over component)

6 Types of event listeners (2) Component-specific listeners relevant to specific components actions Types ActionListener CaretListener ChangeListener DocumentListener ItemListener ListSelectionListener WindowListener etc. See: http://java.sun.com/docs/books/tutorial/uiswing/events/eventsandcomponent s.html http://java.sun.com/docs/books/tutorial/uiswing/events/eventsandcomponent s.html

7 Getting event information EventObject class - use sub classes of this to determine whats happened. Get the firing object with getSource(); Actual event classes sometimes have specific types e.g. the ComponentListener uses a sub-class of EventObject : ComponentEvent that has getComponent(); Event classes may define methods that return more information e.g. ActionEvent has a method for getting modifiers (Shift, Alt, Ctrl)

8 Low-level and semantic events (1) Low-level events - window-system level e.g. mouse, key, component, container, focus, window trigger component-independent Semantic events everything else! – e.g. action, item, list selection trigger can differ by component e.g. button click and textfield return action events

9 Adapters for event handling (1) Classes which implement listener interfaces must implement all listener methods e.g. MouseListener has 5 methods: mouseClicked, mouseReleased, mousePressed, mouseEntered, mouseExited This leads to cluttered code Say you only want mouseClicked to do something then all others have to be implemented but empty Alternative….


Download ppt "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."

Similar presentations


Ads by Google