Event handling and listeners What is an event? user actions and context event sources and listeners...

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

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.

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.

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.

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.

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.

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.

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.

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.

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.

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….