Upload
rosemary-price
View
215
Download
0
Embed Size (px)
DESCRIPTION
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
Citation preview
Event HandlingCS 21a: Introduction to
Computing IFirst Semester, 2013-2014
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
Buttons and Listeners►To program functionality for a button, you
need to associate the button object to a listener objectprivate 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
ActionListener► In the java library (java.awt.event.*), there is an
interface called ActionListenerpublic 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 interfacepublic class SomeListener implements ActionListener{ public void actionPerformed( ActionEvent ae ) { // contains statements to be executed when // the button is clicked }}
Define Listener Classesas 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
Button Listenerprivate 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 fieldsof the frame/applet
Need access to these fields within the listener class
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
Organizing Applet orFrame 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
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 fieldso all methodscould refer to it
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