Professional Documents
Culture Documents
Event Handling: Making Those Guis Do Something!
Event Handling: Making Those Guis Do Something!
A look at ActionListener
A quick look at the documentation reveals that ActionListener is very simple. It has one method:
public void actionPerformed(ActionEvent e);
To implement this interface, we just need to write a class with a method like this.
9
11
Button Presses
A simple form of event is the pressing of a button. This generates an ActionEvent, which we listen for with an ActionListener. Let's write one!
12
Simple ActionListener
implements keyword
public class FirstActionListener implements ActionListener { public void actionPerformed(ActionEvent e) { System.out.println("Event handled!"); } } code to run when the button is pressed
13
Using it on a GUI
1. Create a button:
JButton button = new JButton("Press me");
2. Create a listener:
ActionListener listener = new FirstActionListener();
14
15
16
This makes sense because our GUI is a special kind of window, i.e. one with a button on it! It will turn out to be a useful design later.
17
20
22
The rest is just as before. It works! This is a very important example. Please make sure you understand it.
24
CountListener
public class CountListener implements ActionListener { private JTextField output; private int count; public CountListener(JTextField out) { count = 0; output = out; } continued on next slide
26
CountListener continued
public void actionPerformed (ActionEvent e) { count = count + 1; output.setText("Number of presses: " + count); } }
27
It works, but
This program works, but the user can type text into the textfield. This is not what we want, because we're using the textfield for output, not input. You can fix it with
display.setEditable(false);
28
Getting input
A common thing to want to do is to get input from the user and do something interesting with it. We will now try this, but we will be doing something very boring with the input: measuring the length of the input string, and displaying the answer.
29
The GUI
We will use a very simple GUI: two textfields (one for input, one for output) and a button to say "do it!".
30
GUI code
Container pane=this.getContentPane(); pane.setLayout(new FlowLayout()); JTextField input = new JTextField(20); pane.add(input); JButton button = new JButton("Calculate length"); pane.add(button); JTextField display=new JTextField(20); display.setEditable(false); pane.add(display);
31
Adding a listener
We need to set things up so that when the user presses the button, our program
1. fetches the text from the first textfield 2. counts the length of this string 3. displays this number in the second textfield
Listener design
What information will our listener need to store to do this work? It needs to know
where to find the input where to send the output
It does not need to store the input anywhere, or remember anything from one press to the next.
33
As it happens, they are going to contain (point to) the same JTextField object. They are not the same variable, and will not contain the same thing unless we make it so. Let's make it so!
35
36
Shorter version
Those three lines can be abbreviated to:
output.setText("Length is " + input.getText().length());
37
39
Summary
To respond to events, we create listener objects. A listener is an object whose class implements an appropriate interface. ActionListener is the interface used to listen for button presses. The method actionPerformed contains the code to run when a button is pressed. The listener is attached to a GUI component using addActionListener.
40