A simple JavaFX tutorial through simple snippets.
Example 1: Properties
Here are the code:
Step 1: Create Project
- Open your favorite Java IDE.
- In the menu go to
File --> Create New Project
.
Step 2: Dependencies
No dependencies are needed for this project.
Step 3: Write Code
Our code will comprise the following java files:
PropertyExample.java
- In your editor or IDE, create a file known as
PropertyExample.java
. - Then add the following code:
(a). PropertyExample.java
Go ahead and add the following imports:
import javafx.application.Application;
import javafx.beans.property.DoubleProperty;
import javafx.beans.property.ReadOnlyDoubleProperty;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
We will need to extend our class using the extend
keyword. By doing that our class
can make use of inheritance to derive properties and functions defined in the parent class
.
public class PropertyExample extends Application {
Our class
will have the following methods:
void main(String[] args)
void start(Stage primaryStage)
void changed(ObservableValue<? extends Number> observableValue, Number oldVal, Number newVal)
First of all we will need a main method. This is the entry point of our Java application.
public static void main(String[] args) {
In this particular class
we will be overriding our void changed(ObservableValue<? extends Number> observableValue, Number oldVal, Number newVal)
method.
Prepend the code>@Override</code modifier to your method. Then add implementation code as follows:
@Override
public void changed(ObservableValue<? extends Number> observableValue, Number oldVal, Number newVal) {
System.out.println("widthProperty changed from " + oldVal.doubleValue() + " to " + newVal.doubleValue());
}
Here is the full code:
package com.jenkov.javafx.properties;
import javafx.application.Application;
import javafx.beans.property.DoubleProperty;
import javafx.beans.property.ReadOnlyDoubleProperty;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
public class PropertyExample extends Application {
public static void main(String[] args) {
launch(args);
}
public void start(Stage primaryStage) {
Pane pane = new Pane();
ReadOnlyDoubleProperty widthProperty = pane.widthProperty();
widthProperty.addListener( new ChangeListener<Number> (){
@Override
public void changed(ObservableValue<? extends Number> observableValue, Number oldVal, Number newVal) {
System.out.println("widthProperty changed from " + oldVal.doubleValue() + " to " + newVal.doubleValue());
}
});
DoubleProperty prefWidthProperty = pane.prefWidthProperty();
prefWidthProperty.addListener( (ObservableValue<? extends Number> prop, Number oldVal, Number newVal) -> {
System.out.println("prefWidthProperty changed from " + oldVal.doubleValue() + " to " + newVal.doubleValue());
});
prefWidthProperty.set(123);
Scene scene = new Scene(pane, 1024, 800, true);
primaryStage.setScene(scene);
primaryStage.setTitle("2D Example");
primaryStage.show();
}
}
Download
Download the code using the below links:
Number | Link |
---|---|
1. | Download Example |
2. | Follow code author |
3. | Code: Apache 2.0 License |