GUI Event Handling: Nithya Raman
GUI Event Handling: Nithya Raman
GUI Event Handling: Nithya Raman
Nithya Raman
What is an Event?
GUI components communicate with the rest of the applications through events. The source of an event is the component that causes that event to occur. The listener of an event is an object that receives the event and processes it appropriately.
Handling Events
Every time the user types a character or clicks the mouse, an event occurs. Any object can be notified of any particular event. To be notified for an event,
The object has to be registered as an event listener on the appropriate event source. The object has to implement the appropriate interface.
public class SwingApplication implements ActionListener { ... JButton button = new JButton("I'm a Swing button!"); button.addActionListener(this); .... public void actionPerformed(ActionEvent e) { numClicks++; label.setText(labelPrefix + numClicks); } }
Types of Events
Below, are some of the many kinds of events, swing components generate.
Act causing Event Listener Type
ActionListener
WindowListener MouseListener
MouseMotionListener
ComponentListener
ListSelectionListener
Event Listeners
Event listeners are the classes that implement the <type>Listener interfaces. Example:
1. ActionListener receives action events 2. MouseListener receives mouse events.
The following slides give you a brief overview on some of the listener types.
The above code contains the handler for the ActionEvent e that occurred.
Invoked when the mouse cursor has been moved onto a component but no buttons have been pushed.
public void mouseMoved(MouseEvent e)
Invoked when the user attempts to close the window object from the objects system menu.
public void windowClosing(WindowEvent e)
Invoked when the window is changed from the minimized state to the normal state.
public void windowDeconified(WindowEvent e)
Note: The number of event objects is much greater then specified in diagramDue to space constraints, only some of them are represented in the figure
Courtesy: Safari.oreilly.com
The main purpose of the last few slides is to give you an idea as to how you can use event handlers in your programs. It is beyond the scope of the OReilly book to cover every event handler. See the JAVA tutorials for more information.
Adapter classes
Built-in in JAVA Implement all the methods of each listener interface with more than one method. Implementation of all empty methods
Illustration (contd..)
public class MyClass implements MouseListener { ... someObject.addMouseListener(this); /* Empty method definition. */ public void mousePressed(MouseEvent e) { } /* Empty method definition. */ public void mouseReleased(MouseEvent e) { } /* Empty method definition. */ public void mouseEntered(MouseEvent e) { } /* Empty method definition. */ public void mouseExited(MouseEvent e) { } public void mouseClicked(MouseEvent e) { //Event listener implementation goes here... } }
Illustration (contd..)
To help you avoid implementing empty bodies, the API generally includes an adapter class for each listener interface with more than one method. For example, the MouseAdapter class implements the MouseListener interface.
public
Design Considerations
The most important rule to keep in mind about event listeners is that they must execute quickly. Because, all drawing and eventlistening methods are executed in the same thread, a slow event listener might make the program seem unresponsive. So, consider the performance issues also when you create event handlers in your programs.
Design Considerations
You can have choices on how the event listener has to be implemented. Because, one particular solution might not fit in all situations. For example, you might choose to implement separate classes for different types of listeners. This might be a relatively easy architecture to maintain, but many classes can also result in reduced performance .
Make sure you have registered the right kind of listener to detect the events. Make sure you have registered the listener on the right object. Make sure you have implemented the event handler correctly, especially, the method signatures.
References
Jia, Xiaoping, Object Oriented Software Development Using Java. Addison Wesley, 2003 http://java.sun.com/docs/books/tutorial/uiswing/events/in dex.html http://java.sun.com/docs/books/tutorial/uiswing/learn/exa mple2.html#handlingEvents http://safari.oreilly.com/0672315467/ch09