Event handling and listeners What is an event? user actions and context event sources and listeners...
-
Upload
erin-holland -
Category
Documents
-
view
212 -
download
0
Transcript of Event handling and listeners What is an event? user actions and context event sources and listeners...
![Page 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.](https://reader036.fdocuments.in/reader036/viewer/2022082917/5513ed5d55034674748b5a46/html5/thumbnails/1.jpg)
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
![Page 2: 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.](https://reader036.fdocuments.in/reader036/viewer/2022082917/5513ed5d55034674748b5a46/html5/thumbnails/2.jpg)
Some typical component events and listeners
Act that results in event Listener
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
![Page 3: 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.](https://reader036.fdocuments.in/reader036/viewer/2022082917/5513ed5d55034674748b5a46/html5/thumbnails/3.jpg)
Implementing listeners (1)
• Three key bits of code 1) add interface 2) register 3) handle
• Components can have multiple listeners
• A simple JButton ActionListener…
![Page 4: 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.](https://reader036.fdocuments.in/reader036/viewer/2022082917/5513ed5d55034674748b5a46/html5/thumbnails/4.jpg)
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
![Page 5: 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.](https://reader036.fdocuments.in/reader036/viewer/2022082917/5513ed5d55034674748b5a46/html5/thumbnails/5.jpg)
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)
![Page 6: 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.](https://reader036.fdocuments.in/reader036/viewer/2022082917/5513ed5d55034674748b5a46/html5/thumbnails/6.jpg)
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/eventsandcompone
nts.html
![Page 7: 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.](https://reader036.fdocuments.in/reader036/viewer/2022082917/5513ed5d55034674748b5a46/html5/thumbnails/7.jpg)
Getting event information
• EventObject class - use sub classes of this to determine what’s 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)
![Page 8: 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.](https://reader036.fdocuments.in/reader036/viewer/2022082917/5513ed5d55034674748b5a46/html5/thumbnails/8.jpg)
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
![Page 9: 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.](https://reader036.fdocuments.in/reader036/viewer/2022082917/5513ed5d55034674748b5a46/html5/thumbnails/9.jpg)
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….