Kensoft PH
  • Download
    • KenshotApplication
  • Contact
  • About
Java Quiz
No Result
View All Result
Kensoft PH
  • Download
    • KenshotApplication
  • Contact
  • About
Java Quiz
No Result
View All Result
Kensoft PH
No Result
View All Result
Home Java

JavaFX Mouse Events Tutorial – 100% Best For Beginners

November 9, 2021
in Java
Reading Time: 4 mins read
0
JavaFX Mouse Events 2
2.5k
VIEWS
Share on FacebookShare on TwitterShare via Email

Contents

Toggle
  • JavaFX Mouse Events Tutorial
  • Types of Mouse Events in JavaFX
  • Handling Events
    • Event filters and handlers
    • Using onXXX convenience properties
      • Example: Mouse Clicked Event
      • Output
      • Example: Mouse Pressed Event
      • Output
  • YouTube Video

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

YouTube video

Tags: JavaFX Mouse EventsMouse Events in JavaFX
Previous Post

Rank Math Pro Review – 100% Best SEO Plugin

Next Post

How To install JDK 17 and JavaFX 17 on NetBeans IDE | Best

KENSOFT

KENSOFT

What’s up! Kent is my name. The name KENSOFT is derived from the words Kent and Software. My programming language of choice is Java

Related tutorials

How to Use the JavaFX Pie Chart 100% For Beginners
Java

How to Use the JavaFX Pie Chart 100% For Beginners

June 12, 2024 - Updated on October 6, 2024
205
How to Connect to an API Using JavaFX
Java

How to Connect to an API Using JavaFX

May 26, 2024 - Updated on September 28, 2024
215
JavaFX SQLite Database CRUD Tutorial
Java

JavaFX SQLite Database CRUD Tutorial | Note Application

May 26, 2024 - Updated on September 28, 2024
586
Next Post
JavaFX 17

How To install JDK 17 and JavaFX 17 on NetBeans IDE | Best

Key Event in Java

How to use the Key Event in JavaFX | 100% Best for beginners

Window Event in JavaFX

Window event in JavaFX | 100% Best for beginners

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Tools

Multi-platform installer builder

Java profiler

  • Trending
  • Comments
  • Latest
MySQL database using XAMPP

How to connect Java to MySQL database using Xampp server | 100% best for beginners

October 27, 2020 - Updated on January 23, 2023
Failed to automatically set up a JavaFX Platform

Failed to automatically set up a JavaFX Platform SOLVED Apache NetBeans 12.3 | Best way

April 11, 2021 - Updated on July 3, 2022
JavaFX 17

How To install JDK 17 and JavaFX 17 on NetBeans IDE | Best

November 15, 2021 - Updated on December 13, 2021
hide and show password in jPasswordField

JPasswordField in Java Hide or Show Password | 100% best for beginners

April 2, 2021 - Updated on September 21, 2022
Failed to automatically set up a JavaFX Platform

Failed to automatically set up a JavaFX Platform SOLVED Apache NetBeans 12.3 | Best way

3DES in Java and AES in Java

How to use AES and 3DES in Java | 100% best for beginners

JavaFX Splash Screen

How to create JavaFX Splash Screen | 100% best for beginners

set up JavaFX and Scene Builder

How to set up JavaFX and Scene Builder in NetBeans IDE | 100% best for beginners

How to Use the JavaFX Pie Chart 100% For Beginners

How to Use the JavaFX Pie Chart 100% For Beginners

June 12, 2024 - Updated on October 6, 2024
How to Connect to an API Using JavaFX

How to Connect to an API Using JavaFX

May 26, 2024 - Updated on September 28, 2024
JavaFX SQLite Database CRUD Tutorial

JavaFX SQLite Database CRUD Tutorial | Note Application

May 26, 2024 - Updated on September 28, 2024
How to take a screenshot on PC using Kenshot

How to Take a Screenshot on PC Using Kenshot: A Full Guide

January 18, 2024 - Updated on October 6, 2024

Latest Tutorials

How to Use the JavaFX Pie Chart 100% For Beginners

How to Use the JavaFX Pie Chart 100% For Beginners

June 12, 2024 - Updated on October 6, 2024
How to Connect to an API Using JavaFX

How to Connect to an API Using JavaFX

May 26, 2024 - Updated on September 28, 2024
JavaFX SQLite Database CRUD Tutorial

JavaFX SQLite Database CRUD Tutorial | Note Application

May 26, 2024 - Updated on September 28, 2024

Popular Tutorials

  • MySQL database using XAMPP

    How to connect Java to MySQL database using Xampp server | 100% best for beginners

    0 shares
    Share 0 Tweet 0
  • Failed to automatically set up a JavaFX Platform SOLVED Apache NetBeans 12.3 | Best way

    0 shares
    Share 0 Tweet 0
  • How To install JDK 17 and JavaFX 17 on NetBeans IDE | Best

    0 shares
    Share 0 Tweet 0
Facebook Instagram Youtube Github LinkedIn Discord
Kensoft PH

What’s up! I'm Kent. The name KENSOFT is derived from the words Kent and Software. My programming language of choice is Java, which I use to create computer applications. In a company, I created applications and a website.

Categories

Website

Check the status

Privacy Policy

Terms and Condition

Sitemap

Latest Tutorials

How to Use the JavaFX Pie Chart 100% For Beginners

How to Use the JavaFX Pie Chart 100% For Beginners

June 12, 2024 - Updated on October 6, 2024
How to Connect to an API Using JavaFX

How to Connect to an API Using JavaFX

May 26, 2024 - Updated on September 28, 2024
JavaFX SQLite Database CRUD Tutorial

JavaFX SQLite Database CRUD Tutorial | Note Application

May 26, 2024 - Updated on September 28, 2024

© 2024 Made With Love By KENSOFT PH

No Result
View All Result
  • Download
    • Kenshot
  • Contact
  • About
  • Java Quiz

© 2024 Made With Love By KENSOFT PH

This website uses cookies. By continuing to use this website you are giving consent to cookies being used. Visit our Privacy and Cookie Policy.