The main change is in the event handling - which affects any program that responds to clicks on scroll bars, buttons, terxt fields - all the widgets. We give below extracts from the new chapter on events - those extracts that explain the new event handling. Or you could go to Sun's explanation of how to convert to the new event handling at:
http://java.sun.com:80/products/jdk/1.1/docs/guide/awt/HowToUpgrade.html#events
There are other minor changes:
In Java, events are classified . For example, scrollbars are typically used to modify or adjust a value or screen area, whereas buttons are clicked to initiate or terminate a task - a definite action, rather than an adjustment. In Java, these classes are named ActionListener and AdjustmentListener. Our program must register as a listener for particular classes of event.
Here is an analogy - assume you are interested in music CDs from a particular company, who produce rock, Latin, and jazz recordings You might register with them by saying 'please send me details when you release a batch of new Latin CDs'. When the event of the CD release happens, the company will send you a mailshot.
In Java, we have to code a method for every class of event in which we are interested. (e.g. a method for scrollbar events, a method for button-click events ). Additionally, we have to register as a listener with the component (e.g. scrollbar) which produces the event, so that our method can be invoked automatically when the event happens.
A first event-driven program - a scroll bar applet
First we will look at a very simple example - a program to read a number from a scrollbar and display its value as digits on the screen.
import java.awt.*; import java.applet.Applet; import java.awt.event.*; public class FirstEvent extends Applet implements AdjustmentListener { private Scrollbar slider; private int sliderValue = 0; public void init() { slider = new Scrollbar(Scrollbar.HORIZONTAL ,0, 1, 0, 100); add(slider); slider.addAdjustmentListener(this); } public void paint(Graphics g) { g.drawString("Current value is " + sliderValue, 100,100); } public void adjustmentValueChanged(AdjustmentEvent e) { sliderValue = slider.getValue(); repaint(); } }
Let us consider the task of the program in terms of events. (Later we will introduce more detail.) The user will manipulate a scrollbar and the number it is set to will be displayed on the screen as digits.
Rather than producing a program which examines the scrollbar very frequently (say every 1/10th of a second), we use the event-driven approach. We state that we are going to implement a listener for scrollbar events, which are classified in AdjustmentListener. Note the capital 'A' - this is a class, not a method. We then register our program as a listener by using the addAdjustmentListener method, which in effect causes our adjustmentValueChanged method to be invoked when the scrollbar is used. In this simple program, our adjustmentValueChanged method simply fetches the new value from the scrollbar, and arranges for it to be painted on the screen.
Here is the essence of the program, expressed in informal English. Trust us that the Java detail will follow!
declare a variable, sliderValue, initially 0 init: set up the scrollbar so that it supplies us with integers from 0 to 100 register as a listener to the scrollbar paint: display the current value of sliderValue as a number adjustmentValueChanged: get the current value of the scrollbar and place it in sliderValue. invoke the paint method, by using repaint()
There is only one thing you can do with this program (apart from stopping it). You can manipulate the scrollbar. Every time you do this, adjustmentValueChanged gets the new value, and deposits it in a variable (sliderValue) for paint to display.
There are some essential unchangeable parts of the program (with changes for new event handling announced in italics):
When we say unchangeable, we mean it. Don't invent your own names, such as initialize.
An event-driven example - the window blind
The following program simulates an electronic window blind - when we drag the vertical scrollbar downwards, the window blind lowers (see graphic of the screen in first edition). To represent this setup, we draw a rectangle to represent the window frame, and a filled rectangle to represent the blind.
The example pulls together all we have done about scopes, components and events. The example is short, and we will explain it in detail. When you come to write your own event-driven programs you will find that the structure is familiar.
import java.awt.*; import java.applet.Applet; import java.awt.event.*; public class WindowBlind extends Applet implements AdjustmentListener { private Scrollbar slider; private int sliderValue; public void init () { slider = new Scrollbar(Scrollbar.VERTICAL,0,1,0,100); add(slider); slider.addAdjustmentListener(this); } public void paint (Graphics g) { showStatus("Scrollbar value is " + sliderValue); g.drawRect(40, 80, 60, 100); g.fillRect(40, 80, 60, sliderValue); } public void adjustmentValueChanged(AdjustmentEvent e) { sliderValue = slider.getValue(); repaint(); } }
The overall structure consists of three methods, required in most applets. There is an init method, a paint method, and an adjustmentValueChanged method. The unusual thing about them is that we provide them but donít explicitly make use of them - in fact, paint and init are invoked by the appletviewer or browser, and adjustmentValueChanged is invoked by the Java event-handling system.
Let us walk through the applet. We import the event-handling classes, and state that our applet will provide a method for dealing with adjustment events such as those produced from scrollbars:
import java.awt.event.*; public class WindowBlind extends Applet implements AdjustmentListener {
Then we declare a scrollbar called slider as a private instance variable, so that any method of the WindowBlind class can access it. We are free to choose its name, and could have opted for bar, scroller or blind for example. We follow the pattern of declaring it, creating the instance, adding it to the screen, and registering with it:
private Scrollbar slider; //etc. slider = new Scrollbar(Scrollbar.VERTICAL, 0, 1, 0, 100); add(slider); slider.addAdjustmentListener(this);
Registration should be regarded as a formality that we must do, but the behind-the-scenes process (which you don't need to understand at this stage) is rather intricate. When we put:
slider.addAdjustmentListener(this);
we are stating that our current program (in fact the current object, referred to in Java by this) requires that its adjustmentValueChanged method is to be invoked when slider is manipulated.
We prepare the program for events, in the sense of setting up methods that it will invoke. In this case we must provide an adjustmentValueChanged method. We have:
public void adjustmentValueChanged(AdjustmentEvent e) { sliderValue = slider.getValue(); repaint(); }
What happens is that the above method is invoked, and details about the event are available in the variable e, of class AdjustmentEvent. In our primitive program there is only one event (the scrollbar is changed), so all we do is make use of getValue to fetch the changed position and store this in sliderValue. Then - in recognition that the screen display needs changing - we invoke repaint which, in its basic form, merely calls paint. This causes the drawing of a new window frame and blind.
Further examples of using the new event model anre shown in the sample chapter 7. These use scroll bars, buttons and text fields.
Last updated 01 July 1998. More to be addedd later.