A simple JavaFX tutorial through simple snippets.
Example 1: Scene
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:
SceneCursorExample.java
- In your editor or IDE, create a file known as
SceneCursorExample.java
. - Then add the following code:
(a). SceneCursorExample.java
Go ahead and add the following imports:
import javafx.application.Application;
import javafx.scene.Cursor;
import javafx.scene.Scene;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
Go ahead and add the following imports:
import static javafx.application.Application.launch;
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 SceneCursorExample extends Application {
Our class
will have the following methods:
void main(String[] args)
void start(Stage primaryStage)
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 start(Stage primaryStage)
method.
Prepend the code>@Override</code modifier to your method. Then add implementation code as follows:
@Override
public void start(Stage primaryStage) {
VBox vbox = new VBox();
Scene scene = new Scene(vbox);
//scene.setCursor(Cursor.DEFAULT);
scene.setCursor(Cursor.CROSSHAIR);
//scene.setCursor(Cursor.DISAPPEAR);
//scene.setCursor(Cursor.CLOSED_HAND);
//scene.setCursor(Cursor.HAND);
//scene.setCursor(Cursor.OPEN_HAND);
//scene.setCursor(Cursor.V_RESIZE);
//scene.setCursor(Cursor.H_RESIZE);
//scene.setCursor(Cursor.N_RESIZE);
//scene.setCursor(Cursor.NE_RESIZE);
//scene.setCursor(Cursor.E_RESIZE);
//scene.setCursor(Cursor.SE_RESIZE);
//scene.setCursor(Cursor.S_RESIZE);
//scene.setCursor(Cursor.SW_RESIZE);
//scene.setCursor(Cursor.W_RESIZE);
//scene.setCursor(Cursor.NW_RESIZE);
primaryStage.setTitle("Scene Example");
primaryStage.setHeight(300);
primaryStage.setWidth(300);
primaryStage.setScene(scene);
primaryStage.show();
}
Here is the full code:
package com.jenkov.javafx.scene;
import javafx.application.Application;
import javafx.scene.Cursor;
import javafx.scene.Scene;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import static javafx.application.Application.launch;
/**
This example shows how to set a specific mouse cursor
*/
public class SceneCursorExample extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
VBox vbox = new VBox();
Scene scene = new Scene(vbox);
//scene.setCursor(Cursor.DEFAULT);
scene.setCursor(Cursor.CROSSHAIR);
//scene.setCursor(Cursor.DISAPPEAR);
//scene.setCursor(Cursor.CLOSED_HAND);
//scene.setCursor(Cursor.HAND);
//scene.setCursor(Cursor.OPEN_HAND);
//scene.setCursor(Cursor.V_RESIZE);
//scene.setCursor(Cursor.H_RESIZE);
//scene.setCursor(Cursor.N_RESIZE);
//scene.setCursor(Cursor.NE_RESIZE);
//scene.setCursor(Cursor.E_RESIZE);
//scene.setCursor(Cursor.SE_RESIZE);
//scene.setCursor(Cursor.S_RESIZE);
//scene.setCursor(Cursor.SW_RESIZE);
//scene.setCursor(Cursor.W_RESIZE);
//scene.setCursor(Cursor.NW_RESIZE);
primaryStage.setTitle("Scene Example");
primaryStage.setHeight(300);
primaryStage.setWidth(300);
primaryStage.setScene(scene);
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 |