JavaFX Mouse Events – Kensoft PH https://kensoftph.com Power up your knowledge in programming Tue, 09 Nov 2021 07:23:27 +0000 en-US hourly 1 https://wordpress.org/?v=6.7.1 https://kensoftph.com/wp-content/uploads/2021/07/cropped-Kensoftph-New-logo-32x32.png JavaFX Mouse Events – Kensoft PH https://kensoftph.com 32 32 JavaFX Mouse Events Tutorial – 100% Best For Beginners https://kensoftph.com/javafx-mouse-events-tutorial-for-beginners/ https://kensoftph.com/javafx-mouse-events-tutorial-for-beginners/#respond Tue, 09 Nov 2021 07:23:26 +0000 https://kensoftph.com/?p=1633 JavaFX Mouse Events Tutorial

JavaFX Mouse Events are used to handle mouse events. The MouseEvents works when you Clicked, Dragged, or Pressed and etc.  An object of the MouseEvent class represents a mouse events. This tutorial is ideal for learning as a beginner, but if you are an experienced programmer, you can continue reading and learning more.

Learning mouse events in JavaFX is easy. I will also show you the lists of mouse events in JavaFX so you can understand the different types of events and their descriptions. The different types of mouse events are listed below.

Types of Mouse Events in JavaFX

  • ANY – This mouse event type is known as the supertype of all mouse event types. If you want your node to receive all types of events. This event type would be used for your handlers.
  • MOUSE_PRESSED – When you press a mouse button, this event is triggered. The MouseButton enum defines three constants that represent a mouse button: NONE, PRIMARY, and SECONDARY. The MouseEvent class’s getButton() method returns the mouse button that is responsible for the event.
  • MOUSE_RELEASED – The event is triggered if you pressed and released a mouse button in the same node.
  • MOUSE_CLICKED – This event will occur when you pressed and released a node.
  • MOUSE_MOVED – Simply move your mouse without pressing any mouse buttons to generate this type of mouse event.
  • MOUSE_ENTERED – This event occurs when the mouse or cursor enters the target node.
  • MOUSE_EXITED – This event occurs when the mouse or cursor leaves or moved outside the target node.
  • MOUSE_DRAGGED – This event occurs when you move the mouse with a pressed mouse button to a target node.

JavaFX Mouse Events

Handling Events

The handling event means that when you execute the application login in response to the occurrence of the event. The application logic is contained in the event filter and handler, both of which are EventHandler interface objects. Both event filter and handler are objects of the same EventHandler interface, and it is impossible to determine whether an EventHandler object is a filter or a handler simply by looking at it. You can register the same EventHandler objects as an event filter as well as event handler at the same time by using the addEventFilter() and addEventHanlder() methods.

Event filters and handlers

Creating the event filter and handler is as simple as creating objects of the class that implement the EventHandler interface. If you want a node, let’s say for example a button. If you want your button to process events of specific types, you will need to register event filters and handlers for those event types to the button.

import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;

public class MouseEvents extends Application {

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

    @Override
    public void start(Stage stage) {
        Circle circle = new Circle(100, 100, 50);
        circle.setFill(Color.CORAL);
        // Create a MouseEvent filter
        EventHandler<MouseEvent> mouseEventFilter
                = e -> System.out.println("Mouse event filter has been called.");
        // Create a MouseEvent handler
        EventHandler<MouseEvent> mouseEventHandler
                = e -> System.out.println("Mouse event handler has been called.");
        // Register the MouseEvent filter and handler to the Circle
        // for mouse-clicked events
        circle.addEventFilter(MouseEvent.MOUSE_CLICKED, mouseEventFilter);
        circle.addEventHandler(MouseEvent.MOUSE_CLICKED, mouseEventHandler);
        HBox root = new HBox();
        root.getChildren().add(circle);
        Scene scene = new Scene(root);
        stage.setScene(scene);
        stage.setTitle("Registering Event Filters and Handlers");
        stage.show();
        stage.sizeToScene();
    }
}

Mouse Events in JavaFX

Using onXXX convenience properties

This method is more convenient for registering an event handler for mouse events in JavaFX because you can simply use the setOnXXX() methods of these properties to register an event handler. For example, to register an event handler for the mouse clicked event, use the setOnMouseClicked() method.

Mouse Events in JavaFX

Example: Mouse Clicked Event

In this example, you will learn how to use JavaFX’s setOnMouseClicked() convenient properties. Please proceed to the code provided below.

button.setOnMouseClicked(event ->{
    System.out.println("Mouse Clicked");
});

Output

Mouse Clicked

Example: Mouse Pressed Event

Also with the Mouse pressed event. Learning JavaFX Mouse events is very easy.

button.setOnMousePressed(event ->{
    System.out.println("Mouse Pressed");
});

Output

Mouse Pressed

YouTube Video

]]>
https://kensoftph.com/javafx-mouse-events-tutorial-for-beginners/feed/ 0 JavaFX Mouse Events Tutorial For Beginners nonadult