Download presentation
Presentation is loading. Please wait.
Published byRosemary Price Modified over 8 years ago
1
Event Handling CS 21a: Introduction to Computing I First Semester, 2013-2014
2
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
3
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
4
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 } }
5
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
6
Button Listener 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
7
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
8
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
9
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 ActionListener { public void actionPerformed( ActionEvent ae ) { String text = textField.getText(); label.setText( “Hello,”+text ); } } button.addActionListener( new ButtonListener() ); } define Container c; as an instance field so all methods could refer to it
10
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
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.