Saturday, July 13, 2013

JavaFX example: Set cursor of nodes

The setCursor(Cursor value) method of javafx.scene.Node set the mouse cursor for this Node and subnodes. The javafx.scene.Cursor class encapsulate bitmaps of various mouse cursor you can use.

Example:



package javafx_cursor;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Cursor;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

/**
 *
 * @web http://java-buddy.blogspot.com/
 */
public class JavaFX_Cursor extends Application {
    
    @Override
    public void start(Stage primaryStage) {
        Button btn = new Button();
        btn.setText("Say 'Hello World'");
        btn.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                System.out.println("Hello World!");
            }
        });
        
        StackPane root = new StackPane();
        root.getChildren().add(btn);
        
        btn.setCursor(Cursor.OPEN_HAND);
        root.setCursor(Cursor.CROSSHAIR);
        
        Scene scene = new Scene(root, 300, 250);
        
        primaryStage.setTitle("java-buddy");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}


1 comment:

  1. how to get html attributes based on the mouse cursor in javafx. Could you please suggest how to do

    ReplyDelete