Java Reference
In-Depth Information
Let's examine a few key portions of this program. First, notice how buttons are created
by these two lines:
This creates two text-based buttons. The first displays the string Up; the second displays
Down.
Next, an action event handler is set for each of these buttons. The sequence for the Up
button is shown here:
As explained, buttons respond to events of type ActionEvent . To register a handler for
these events, the setOnAction( ) method is called on the button. It uses an anonymous in-
ner class to implement the EventHandler interface. (Recall that EventHandler defines
only the handle( ) method.) Inside handle( ) , the text in the response label is set to reflect
the fact that the Up button was pressed. Notice that this is done by calling the setText( )
method on the label. Events are handled by the Down button in the same way.
After the event handlers have been set, the response label and the buttons btnUp and
btnDown are added to the scene graph by using a call to addAll( ) :
The addAll( ) method adds a list of nodes to the invoking parent node. Of course, these
nodes could have been added by three separate calls to add( ) , but the addAll( ) method is
more convenient to use in this situation.
There are two other things of interest in this program that relate to the way the controls
are displayed in the window. First, when the root node is created, this statement is used:
Here, the FlowPane constructor is passed two values. These specify the horizontal and ver-
tical gap that will be left around elements in the scene. If these gaps are not specified, then
Search WWH ::




Custom Search