Interfaces and Callbacks Core Java

A common pattern in programming is the callback pattern. In this pattern, you want to specify the action that should occur whenever a particular event happens. For example, you may want a particular action to occur when a button is clicked or a menu item isselected. However, because you have not yet seen how to implement user interfaces, we consider a similar but simpler situation.

The javax.swing package contains a Timer class that is useful if you want to be notified whenever a time interval has elapsed. For example, if a part of your program contains a clock, then you can ask to be notified every second so that you can update the clock face.

When you construct a timer, you set the time interval and you tell it what it should do whenever the time interval has elapsed.

How do you tell the timer what it should do? In many programming languages, you supply the name of a function that the timer should call periodically. However, the classes in the Java standard library take an object -oriented approach. You pass an object of some class. The timer then calls one of the methods on that object. Passing an object is more flexible than passing a function because the object can carry additional information.

Of course, the timer needs to know what method to call. The timer requires that you specify an object of a class that implements the ActionListener interface of the java .awt .event

package. Here is that interface:

The timer calls the action Performed method when the time interval has expired.

C++NOTE: As you saw earlier, Java does have the equivalent of function pointers, namely, Method objects. However, they are difficult to use, slower, and cannot be checked fortype safety at compile time. Whenever you would use a function pointer in C++, you should consider using an interface in Java.

Suppose you want to print a message “At the tone, the time is . . .”, followed by a beep, once every 10 seconds. You would define a class that implements the ActionListener interface. You would then place whatever statements you want to have executed inside the action Performed method.

Note the ActionEvent parameter of the actionPerformed method. This parameter gives information about the event, such as the source object that generated it—see Chapter 8 for more information. However, detailed information about the event is not important in this program, and you can safely ignore the parameter.

Next, you construct an object of this class and pass it to the Timer constructor.

The first parameter of the Timer constructor is the time interval that must elapse between notifications, measured in milliseconds. We want to be notified every 10 seconds. The second parameter is the listener object.

Listing below puts the timer and its action listener to work. After the timer is started, the program puts up a message dialog and waits for the user to click the Ok button to stop. While the program waits for the user, the current time is displayed in 10-second intervals.

Be patient when running the program. The “Quit program?” dialog box appears right away, but the first timer message is displayed after 10 seconds.

Note that the program imports the javax.swing.Timer class by name, in addition to importing javax .swing .* and java .util .*. This breaks the ambiguity between javax. swing.Timer and java .util .Timer, an unrelated class for scheduling background tasks.

javax.swing.JOptionPane 1.2

  • static void showMessageDialog(Component parent, Object message)
  • displays a dialog box with a message prompt and an OK button. The dialog is centered over the parent component. If parent is null, the dialog is centered on the screen.

  • Timer(int interval, ActionListener listener)
    constructs a timer that notifies listener whenever interval milliseconds have elapsed.
  • void start()
    starts the timer. Once started, the timer calls actionPerformed on its listeners.
  • void stop()
    stops the timer. Once stopped, the timer no longer calls actionPerformed on its listeners.

javax.awt.Toolkit 1.0

  • static Toolkit getDefaultToolkit()
    gets the default toolkit. A toolkit contains information about the GUI environment.
  • void beep()
    emits a beep sound.

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd Protection Status

Core Java Topics