JavaFX | Cursor class with examples

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 argumentsimportjavafx.application.Application;importjavafx.scene.Scene;importjavafx.scene.control.Button;importjavafx.scene.layout.*;importjavafx.event.ActionEvent;importjavafx.event.EventHandler;importjavafx.scene.control.Label;importjavafx.stage.Stage;importjavafx.scene.Cursor;publicclasscursor_0extendsApplication {// launch the applicationpublicvoidstart(Stage stage){// set title for the stagestage.setTitle("Creating Cursor");// create a stack paneTilePane tilepane =newTilePane();// create a labelLabel label =newLabel("Cursor Example");// add buttontilepane.getChildren().add(label);// create a sceneScene scene =newScene(tilepane,200,200);// create a cursorCursor cursor_ = Cursor.cursor("WAIT");// set cursor for the scenescene.setCursor(cursor_);// set the scenestage.setScene(scene);stage.show();}// Main Methodpublicstaticvoidmain(String args[]){// launch the applicationlaunch(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 sceneimportjavafx.application.Application;importjavafx.scene.Scene;importjavafx.scene.control.Button;importjavafx.scene.layout.*;importjavafx.event.ActionEvent;importjavafx.event.EventHandler;importjavafx.scene.control.Label;importjavafx.stage.Stage;importjavafx.scene.Cursor;publicclasscursor_1extendsApplication {// counter of cursorinti =0;// launch the applicationpublicvoidstart(Stage stage){// set title for the stagestage.setTitle("Creating Cursor");// create a buttonButton button =newButton("cursor");// create a stack paneTilePane tilepane =newTilePane();// create a labelLabel label =newLabel("Cursor Example");// create a cursor with predefined cursorCursor 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 buttontilepane.getChildren().add(button);tilepane.getChildren().add(label);// create a sceneScene scene =newScene(tilepane,200,200);// set cursor for the scenescene.setCursor(cursor_[0]);// action eventEventHandler<ActionEvent> event =newEventHandler<ActionEvent>(){publicvoidhandle(ActionEvent e){if(i == cursor_.length -1)i = -1;// change the cursorscene.setCursor(cursor_[++i]);}};// when button is pressedbutton.setOnAction(event);// set the scenestage.setScene(scene);stage.show();}// Main Methodpublicstaticvoidmain(String args[]){// launch the applicationlaunch(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



