To add a listener to an ObservableList we can call:
myObservableList.addListener(myListChangeListener);
The ListChangeListener requires that you implement a method called "onChanged" to be
called when the list has changed. This looks like:
public void onChanged(ListChangedListener.Change change) {
// handle the change.
}
It is important to note that a change to the list need not be a single change. The change parameter
can contain one or more changes. We must iterate through these changes using the next()
method of the Change class.
Items can be added to the list using the lists "add()" method.
Interesting methods of ObservableList
• add() - Add an item to the list
• size() - Return the size of the list
JavaFX Controls
JavaFX provides a wealth of prebuilt controls. Here we will start to look at some of those available
to us. The controls can be found in the javafx.scene.control package.
JavaFX Button
The Button widget provides the classic button visualization. It can be clicked which will generate
an action that can be caught and handled.
To add an image to the button, we can use the 2
nd
parameter on the constructor which can be an
instance of an ImageView.
For example:
Button button = new Button("", new ImageView(new
Image(getClass().getResourceAsStream("images/play.png"))));
which would build a button that looks like:
If the button already exists (perhaps it was added by Scene Builder), the image can be added using
the "graphic" property.
When the button is pressed, its action event occurs. We can register a listener for it using the
setOnAction() method.
For example:
myButton.setOnAction(new EventHandler<ActionEvent>() {
void handle(ActionEvent event) {
// Handler code here ...
}
});
See also:
• JavaFX ImageView
Page 267
Comentários a estes Manuais