JavaFX MouseEvent

0

JavaFX MouseEvent Handler :

There are many types of mouse events and they can be handled by these handler mathods,

  • setOnMouseClicked() used for mouse click events.
  • setOnMouseMoved() used for mouse move events.
  • setOnMousePressed() used for mouse press events
  • setOnMouseDragged() used for mouse drag events.
  • setOnMouseEntered() used for mouse enter (hover) events.
  • setOnMouseExited() used for mouse exit events.
  • setOnMouseReleased() used for mouse release events.

If you are beginners Then read basic JavaFX Event tutorials.

JavaFX MouseEvent Handling Example :


package javafxtuts;
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.effect.DropShadow;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;

public class Javafxtuts extends Application {

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

  @Override
  public void start(Stage stage) {
      stage.setTitle("http://Javafxtuts.com");
      
	//creating a new button.
      Button button = new Button();
      button.setScaleX(2.5);
      button.setScaleY(2.5);
      button.setText("Mouse Event");
	//register a handler for button.
      button.setOnMouseEntered(new EventHandler() {

          @Override
          public void handle(MouseEvent event) {
		// set shadow effect using Dropshadow() as 
              button.setEffect(new DropShadow());
          }

          

       });
      button.setOnMouseExited(new EventHandler(){

          @Override
          public void handle(MouseEvent event) {
              button.setEffect(null);
          }
      
      });
      BorderPane root = new BorderPane();
      
      root.setCenter(button);
        Scene scene = new Scene(root, 500, 600);
        stage.setScene(scene);
        stage.show();
  }
}
Share.

Leave A Reply