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 application   Âpublicvoidstart(Stage stage)   Â{        Â       Â// set title for the stage       Âstage.setTitle("Creating Cursor");       Â// create a stack pane       ÂTilePane tilepane =newTilePane();       Â// create a label       ÂLabel label =newLabel("Cursor Example");       Â// add button       Âtilepane.getChildren().add(label);       Â// create a scene       ÂScene scene =newScene(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   Âpublicstaticvoidmain(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 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 stage   Âstage.setTitle("Creating Cursor");   Â// create a button   ÂButton button =newButton("cursor");   Â// create a stack pane   ÂTilePane tilepane =newTilePane();   Â// create a label   ÂLabel label =newLabel("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 =newScene(tilepane,200,200);   Â// set cursor for the scene   Âscene.setCursor(cursor_[0]);   Â// action event   ÂEventHandler<ActionEvent> event =     ÂnewEventHandler<ActionEvent>()   Â{       Âpublicvoidhandle(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 Methodpublicstaticvoidmain(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
