Having arranged for the textfield and the button to appear in the window, we need to be able to react to the user clicking the button. As was mentioned earlier, handling events such as mouse clicks, mouse movements, key presses, window iconising, window removal, ... , is an area in which the Java Platform was improved between JDK 1.0 and JDK 1.1. Here we will look at how events are handled in versions of the Java platform from JDK 1.1 onwards.
In order to handle the event of a user clicking on the JButton component, you need to do two things:
If you look at the WWW page javaapi:java/awt/event/ActionListener.html, you will see that in order to implement the java.awt.event.ActionListener interface you just need to have a class that declares one method, a method called actionPerformed that has the header:
public void actionPerformed(ActionEvent pActionEvent)So, here is a class called JButtonListener that implements this interface:
0047: // Stage D: a class whose actionPerformed method. // JButtonListener.java 0048: // writes to standard output. 0049: // Barry Cornelius, 22nd November 1999 0050: import java.awt.event. ActionEvent; 0051: import java.awt.event. ActionListener; 0052: import java.util. Date; 0053: public class JButtonListener implements ActionListener 0054: { 0055: public JButtonListener() 0056: { 0057: } 0058: public void actionPerformed(final ActionEvent pActionEvent) 0059: { 0060: final Date tDate = new Date(); 0061: System.out.println(tDate); 0062: } 0063: } 0064:
The GetDateProg program can create an object of this class in the usual way:
JButtonListener tJButtonListener = new JButtonListener();That satisfies the first requirement given above.
The program also needs to say that this object is going to be responsible for handling the clicks on the button. What we are effectively wanting to do is to say: please execute this object's actionPerformed method whenever there is a click on the JButton component. In order to do this, we need to associate the object that has the actionPerformed method with the JButton object; or, in the jargon of Java, our JButtonListener object needs to be added as a listener for any events associated with the JButton object. This can be done using:
tJButton.addActionListener(tJButtonListener);Because the addActionListener method has been applied to tJButton, the actionPerformed method of the object passed as an argument to addActionListener (i.e., tJButtonListener) will be executed at each click of this JButton component.
Here is the code of this version of the GetDateProg program:
0065: // Stage D: responding to a click of a button. // GetDateProg.java 0066: // Barry Cornelius, 22nd November 1999 0067: import java.awt. BorderLayout; 0068: import java.awt. Container; 0069: import javax.swing. JButton; 0070: import javax.swing. JFrame; 0071: import javax.swing. JTextField; 0072: public class GetDateProg 0073: { 0074: public static void main(final String[] pArgs) 0075: { 0076: final JFrame tJFrame = new JFrame("GetDateProg: Stage D"); 0077: final JTextField tJTextField = new JTextField("hello", 35); 0078: final JButton tJButton = new JButton("Get Date"); 0079: final JButtonListener tJButtonListener = new JButtonListener(); 0080: tJButton.addActionListener(tJButtonListener); 0081: final Container tContentPane = tJFrame.getContentPane(); 0082: tContentPane.add(tJTextField, BorderLayout.NORTH); 0083: tContentPane.add(tJButton, BorderLayout.SOUTH); 0084: tJFrame.pack(); 0085: tJFrame.setVisible(true); 0086: } 0087: }
An example of what happens when the JButton component is clicked is shown in the Figure.
Note that we do not have any precise control over when the actionPerformed method is called: this is at the whim of the person using the program. The act of registering code that will be executed later is sometimes referred to as creating a callback.