Cursor class is a part of JavaFX. Cursor class is used to encapsulate the bitmap representation of the mouse cursor. The cursor class has several predefined cursors that can be used according to the needs of the programmer.
Commonly used Methods:
Method | Explanation |
---|---|
cursor(String s) | returns cursor object of the specified string |
toString() | Returns a string representation for the cursor. |
Below programs will illustrate the use of the cursor class:
- Java program to set some predefined cursor to the by passing string identifier as arguments: This program creates a Cursor named cursor_. The cursor will be set to the scene using the function setCursor().we will create a label. The label will be created inside a scene, which in turn will be hosted inside a stage. The function setTitle() is used to provide title to the stage. Then a tile pane is created, on which addChildren() method is called to attach the label inside the scene. Finally, the show() method is called to display the final results.
// Java program to set some predefined cursor
// to the by passing string identifier as arguments
import
javafx.application.Application;
import
javafx.scene.Scene;
import
javafx.scene.control.Button;
import
javafx.scene.layout.*;
import
javafx.event.ActionEvent;
import
javafx.event.EventHandler;
import
javafx.scene.control.Label;
import
javafx.stage.Stage;
import
javafx.scene.Cursor;
Â
Âpublic
class
cursor_0
extends
Application {
Â
ÂÂ Â Â Â
// launch the application
   Â
public
void
start(Stage stage)
   Â
{
        Â
ÂÂ Â Â Â Â Â Â Â
// set title for the stage
       Â
stage.setTitle(
"Creating Cursor"
);
Â
ÂÂ Â Â Â Â Â Â Â
// create a stack pane
       Â
TilePane tilepane =
new
TilePane();
Â
ÂÂ Â Â Â Â Â Â Â
// create a label
       Â
Label label =
new
Label(
"Cursor Example"
);
Â
ÂÂ Â Â Â Â Â Â Â
// add button
       Â
tilepane.getChildren().add(label);
Â
ÂÂ Â Â Â Â Â Â Â
// create a scene
       Â
Scene scene =
new
Scene(tilepane,
200
,
200
);
Â
ÂÂ Â Â Â Â Â Â Â
// create a cursor
       Â
Cursor cursor_ = Cursor.cursor(
"WAIT"
);
Â
ÂÂ Â Â Â Â Â Â Â
// set cursor for the scene
       Â
scene.setCursor(cursor_);
Â
ÂÂ Â Â Â Â Â Â Â
// set the scene
       Â
stage.setScene(scene);
Â
ÂÂ Â Â Â Â Â Â Â
stage.show();
   Â
}
Â
ÂÂ Â Â Â
// Main Method
   Â
public
static
void
main(String args[])
   Â
{
        Â
ÂÂ Â Â Â Â Â Â Â
// launch the application
       Â
launch(args);
   Â
}
}
Output:
- Java program to set some predefined cursor to the scene: This program creates a Button indicated by the name button respectively. We will create an array of predefined cursors named cursor_. The cursor will be set to the scene using the function setCursor() from the list of predefined cursor cursor_. The button will be created inside a scene, which in turn will be hosted inside a stage. We would create a label. The function setTitle() is used to provide title to the stage. Then a tile pane is created, on which addChildren() method is called to attach the button and label inside the scene. Finally, the show() method is called to display the final results.we would create an event handler to handle the button events. The event handler would be added to the button using setOnAction() function. When the button will be pressed the cursor of the scene will be changed by using the function setCursor().
// Java program to set some predefined
// cursor to the scene
import
javafx.application.Application;
import
javafx.scene.Scene;
import
javafx.scene.control.Button;
import
javafx.scene.layout.*;
import
javafx.event.ActionEvent;
import
javafx.event.EventHandler;
import
javafx.scene.control.Label;
import
javafx.stage.Stage;
import
javafx.scene.Cursor;
Â
Âpublic
class
cursor_1
extends
Application {
Â
Â// counter of cursor
int
i =
0
;
Â
Â// launch the application
public
void
start(Stage stage)
{
   Â
// set title for the stage
   Â
stage.setTitle(
"Creating Cursor"
);
Â
ÂÂ Â Â Â
// create a button
   Â
Button button =
new
Button(
"cursor"
);
Â
ÂÂ Â Â Â
// create a stack pane
   Â
TilePane tilepane =
new
TilePane();
Â
ÂÂ Â Â Â
// create a label
   Â
Label label =
new
Label(
"Cursor Example"
);
Â
ÂÂ Â Â Â
// create a cursor with predefined cursor
   Â
Cursor cursor_[] = {Cursor.CLOSED_HAND, Cursor.CROSSHAIR,
                       Â
Cursor.DEFAULT, Cursor.DISAPPEAR,Â
                       Â
Cursor.E_RESIZE, Cursor.H_RESIZE,Â
                       Â
Cursor.HAND, Cursor.MOVE,Â
                       Â
Cursor.N_RESIZE, Cursor.NE_RESIZE,Â
                       Â
Cursor.NONE, Cursor.NW_RESIZE,Â
                       Â
Cursor.OPEN_HAND, Cursor.SE_RESIZE,Â
                       Â
Cursor.SW_RESIZE, Cursor.TEXT,Â
                       Â
Cursor.V_RESIZE, Cursor.W_RESIZE,
                       Â
Cursor.WAIT};
Â
ÂÂ Â Â Â
// add button
   Â
tilepane.getChildren().add(button);
   Â
tilepane.getChildren().add(label);
Â
ÂÂ Â Â Â
// create a scene
   Â
Scene scene =
new
Scene(tilepane,
200
,
200
);
Â
ÂÂ Â Â Â
// set cursor for the scene
   Â
scene.setCursor(cursor_[
0
]);
Â
ÂÂ Â Â Â
// action event
   Â
EventHandler<ActionEvent> event =Â
     Â
new
EventHandler<ActionEvent>()Â
   Â
{
       Â
public
void
handle(ActionEvent e)
       Â
{
           Â
if
(i == cursor_.length -
1
)
               Â
i = -
1
;
Â
ÂÂ Â Â Â Â Â Â Â Â Â Â Â
// change the cursor
           Â
scene.setCursor(cursor_[++i]);
       Â
}
   Â
};
Â
ÂÂ Â Â Â
// when button is pressed
   Â
button.setOnAction(event);
Â
ÂÂ Â Â Â
// set the scene
   Â
stage.setScene(scene);
Â
ÂÂ Â Â Â
stage.show();
}
Â
Â// Main Method
public
static
void
main(String args[])
{
    Â
ÂÂ Â Â Â
// launch the application
   Â
launch(args);
}
}
Output:
Note: The above programs might not run in an online IDE. Please use an offline compiler.
Reference: https://docs.oracle.com/javafx/2/api/javafx/scene/Cursor.html