Event Handling CS 21a: Introduction to Computing I Department of Information Systems and Computer Science Ateneo de Manila University
Event handling in Java User interaction with applets and frames involves handling events Event examples: Clicking on a button Typing text on a text field Dragging a mouse along an area in the applet/frame Events are handled through listeners Copyright 2008, by the authors of these slides, and Ateneo de Manila University. All rights reserved
Buttons and listeners To program functionality for a button, you need to associate the button object to a listener object private JButton click; … click = new Button( “Click me” ); … click.addActionListener( new SomeListener() ); A listener class needs to be defined The class will contain a method which will be executed when the button is clicked Copyright 2008, by the authors of these slides, and Ateneo de Manila University. All rights reserved
ActionListener In the java library (java.awt.event.*), there is an interface called ActionListener public interface ActionListener { public void actionPerformed( ActionEvent ae ); } The listener object to be associated to a button must be an instance of a class that implements this interface public class SomeListener implements ActionListener { public void actionPerformed( ActionEvent ae ) { // contains statements to be executed when the button is clicked } } Copyright 2008, by the authors of these slides, and Ateneo de Manila University. All rights reserved
Define listener classes as inner classes Inner class: class defined within a class Useful when you need to have access to data within objects of the containing class The inner class is not a public class, and is used only within the containing class/method, often to create just one instance Listeners need access to the visual components within the frame or applet Copyright 2008, by the authors of these slides, and Ateneo de Manila University. All rights reserved
Button listener Instance fields of the frame/applet private JButton button; private JTextField textField; private JLabel label; ... button = new JButton( "Click Me" ); c.add( button ); class ButtonListener implements ActionListener { public void actionPerformed( ActionEvent ae ) String text = textField.getText(); label.setText( “Hello,”+text ); } } button.addActionListener( new ButtonListener() ); Instance fields of the frame/applet Need access to these fields within the listener class Copyright 2008, by the authors of these slides, and Ateneo de Manila University. All rights reserved
Handling multiple buttons When there are multiple buttons to listen to, create a different listener class for each button A different actionPerformed() method is executed for each button Copyright 2008, by the authors of these slides, and Ateneo de Manila University. All rights reserved
Organizing applet or frame code For a complex applet or frame, the init() method or the constructor of the frame could be unmanageably long if all component instantiation, calls to add, and listener classes are defined in that method Better to have separate methods for different visual objects or groups of visual objects The init() method or the constructor then contains calls to these methods Copyright 2008, by the authors of these slides, and Ateneo de Manila University. All rights reserved
Organizing code private Container c; ... public HelloWorldFrame() { c = getContentPane(); createLabel(); createTextField(); createButton(); } ... public void createButton() { button = new JButton( "Click Me" ); c.add( button ); class ButtonListener implements ButtonListener { public void actionPerformed( ActionEvent ae ) { String text = textField.getText(); label.setText( “Hello,”+text ); } c.addActionListener( new ButtonListener() ); Copyright 2008, by the authors of these slides, and Ateneo de Manila University. All rights reserved
Organizing code private Container c; ... public HelloWorldFrame() { c = getContentPane(); createLabel(); createTextField(); createButton(); } ... public void createButton() { button = new JButton( "Click Me" ); c.add( button ); class ButtonListener implements ButtonListener { public void actionPerformed( ActionEvent ae ) { String text = textField.getText(); label.setText( “Hello,”+text ); } c.addActionListener( new ButtonListener() ); define Container c; as an instance field so all methods could refer to it Copyright 2008, by the authors of these slides, and Ateneo de Manila University. All rights reserved
Listeners in other contexts Can use ActionListener and actionPerformed() for JTextFields For mouse operations/movements, use MouseListener or MouseMotionListener For keyboard actions, use KeyListener ActionListeners are sufficient for CS 21a Copyright 2008, by the authors of these slides, and Ateneo de Manila University. All rights reserved