JavaFX

JavaFX Input Event Example

Photo of Andreas PomarolliAndreas PomarolliSeptember 30th, 2016Last Updated: April 24th, 2019
1 1,111 26 minutes read

This is a JavaFX InputEvent example. An input event indicates a user input, for example, clicking the mouse, pressing a key, touching a touch screen, and so forth. JavaFX supports many types of input events. All input event-related classes are in thejavafx.scene.input package.

TheInputEvent class is the superclass of all input event classes. Typically, nodes execute the user-registered input event handlers before taking the default action. If the user event handlers consume the event, nodes do not take the default action.
 
 
 
 

 
Suppose you register key-typed event handlers for a TextField, which consume the event. When you type a character, theTextField will not add and display it as its content. Therefore, consuming input events for nodes gives you a chance to disable the default behavior of theNode.

The following table shows an overview of the whole article:

The following examples use Java SE 7 and JavaFX 2.2.

1. Handling Mouse Events

An object of theMouseEvent class represents a mouse event. TheMouseEvent class defines the following mouse-related event types constants. All constants are of the typeEventType<MouseEvent>.

TheNode class contains the convenienceonXXX properties for most of the mouse event types that can be used to add one event handler of a specific mouse event type for a node:

  • ANY: It is the supertype of all mouse event types. If a node wants to receive all types of mouse events, you would register handlers for this type. TheInputEvent.ANY is the supertype of this event type.
  • MOUSE_PRESSED: Pressing a mouse button generates this event. ThegetButton() method of theMouseEvent class returns the mouse button that is responsible for the event. A mouse button is represented by theNONE,PRIMARY,MIDDLE, andSECONDARY constants defined in theMouseButton enum.
  • MOUSE_RELEASED: Releasing a mouse button generates this event. This event is delivered to the same node on which the mouse was pressed. For example, you can press a mouse button on a circle, drag the mouse outside the circle, and release the mouse button. TheMOUSE_RELEASED event will be delivered to the circle, not the node on which the mouse button was released.
  • MOUSE_CLICKED: This event is generated when a mouse button is clicked on a node. The button should be pressed and released on the same node for this event to occur.
  • MOUSE_MOVED: Moving the mouse without pressing any mouse buttons generates this event.
  • MOUSE_ENTERED: This event is generated when the mouse enters a node. The event capture and bubbling phases do not take place for this event. That is, event filters and handlers of the parent nodes of the event target of this event are not called.
  • MOUSE_ENTERED_TARGET: This event is generated when the mouse enters a node. It is a variant of theMOUSE_ENTERED event type. Unlike theMOUSE_ENTER event, the event capture and bubbling phases take place for this event.
  • MOUSE_EXITED: This event is generated when the mouse leaves a node. The event capture and bubbling phases do not take place for this event, that is, it is delivered only to the target node.
  • MOUSE_EXITED_TARGET: This event is generated when the mouse leaves a node. It is a variant of theMOUSE_EXITED event type. Unlike theMOUSE_EXITED event, the event capture and bubbling phases take place for this event.
  • DRAG_DETECTED: This event is generated when the mouse is pressed and dragged over a node over a platform-specific distance threshold.
  • MOUSE_DRAGGED: Moving the mouse with a pressed mouse button generates this event. This event is delivered to the same node on which the mouse button was pressed, irrespective of the location of the mouse pointer during the drag.

1.1 Getting Mouse Location

1.1.1 The Code

FxInputEventExample1.java

import javafx.application.Application;import javafx.event.EventHandler;import javafx.geometry.Insets;import javafx.scene.Scene;import javafx.scene.control.TextArea;import javafx.scene.input.MouseEvent;import javafx.scene.layout.HBox;import javafx.scene.layout.VBox;import javafx.scene.paint.Color;import javafx.scene.shape.Circle;import javafx.scene.shape.Rectangle;import javafx.stage.Stage;public class FxInputEventExample1 extends Application{// Create the LoggingAreaprivate TextArea loggingArea = new TextArea("");public static void main(String[] args) {Application.launch(args);}@Overridepublic void start(Stage stage) {// Create the CircleCircle circle = new Circle (50, 50, 50);circle.setFill(Color.CORAL);// Create the RectangleRectangle rect = new Rectangle(100, 100);rect.setFill(Color.TAN);// Create the HBoxHBox hbox = new HBox();// Set Padding and Spacing for the HBoxhbox.setPadding(new Insets(20));hbox.setSpacing(20);// Add the children to the HBoxhbox.getChildren().addAll(circle, rect);// Create the VBoxVBox root = new VBox();// Set Padding and Spacing for the VBoxroot.setPadding(new Insets(20));root.setSpacing(20);// Add the children to the VBoxroot.getChildren().addAll(hbox, loggingArea);// Add a MOUSE_CLICKED event handler to the stagestage.addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>(){            public void handle(MouseEvent event)             {            handleMouseMove(event);            }        });// Set the Padding and Border for the VBoxroot.setStyle("-fx-padding: 10;" +"-fx-border-style: solid inside;" +"-fx-border-width: 2;" +"-fx-border-insets: 5;" +"-fx-border-radius: 5;" +"-fx-border-color: blue;");// Create the SceneScene scene = new Scene(root);// Add the Scene to the Stagestage.setScene(scene);// Set the Title of the Stagestage.setTitle("A Mouse Location Example");// Display the Stagestage.show();}public void handleMouseMove(MouseEvent e) {// Get the source and target of the EventString source = e.getSource().getClass().getSimpleName();String target = e.getTarget().getClass().getSimpleName();// Get the Mouse location relative to the event sourcedouble sourceX = e.getX();double sourceY = e.getY();// Get the Mouse location relative to the scenedouble sceneX = e.getSceneX();double sceneY = e.getSceneY();// Get the Mouse location relative to the screendouble screenX = e.getScreenX();double screenY = e.getScreenY();// Log the Informationsthis.loggingArea.appendText("Source=" + source + ",, Location:" + " source(" + sourceX + ", " + sourceY + ")" +", scene(" + sceneX + ", " + sceneY + ")" +", screen(" + screenX + ", " + screenY + ")\n");}}

TheMouseEvent class contains methods to give you the location of the mouse when a mouse event occurs. You can obtain the mouse location relative to the coordinate systems of the event source node, theScene, and the screen. ThegetX() andgetY() methods give the (x, y) coordinates of the mouse relative to the event source node. ThegetSceneX() andgetSceneY() methods give the (x, y) coordinates of the mouse relative to the scene to which the node is added. ThegetScreenX() andgetScreenY() methods give the (x, y) coordinates of the mouse relative to the screen to which the node is added.

1.1.2 The GUI

The above class contains the program to show how to use the methods in theMouseEvent class to know the mouse location. It adds aMOUSE_CLICKED event handler to the stage, and the stage can receive the
notification when the mouse is clicked anywhere in its area.

Run the program and click anywhere in the stage, excluding its title bar if you are running it on the desktop. Each mouse click prints a message describing the source, target, and location of the mouse relative to the source, scene, and screen.

A JavaFX Mouse Location Example
A JavaFX Mouse Location Example

1.2 Representing Mouse Buttons

Typically, a mouse has three buttons. You will also find some that have only one or two buttons. Some platforms provide ways to simulate the missing mouse buttons. TheMouseButton enum in thejavafx.scene.input package contains constants to represent mouse button.

The location of the primary and second mouse buttons depends on the mouse configuration. Typically, for right-handed users, the left and right buttons are configured as the primary and secondary buttons, respectively. For the left-handed users, the buttons are configured in the reverse order. If you have a two-button mouse, you do not have a middle button.

1.3 State of Mouse Buttons

TheMouseEvent object that represents a mouse event contains the state of the mouse buttons at the time the event occurs. TheMouseEvent class contains many methods to report the state of mouse buttons.

In many circumstances, thegetButton() method may returnMouseButton.NONE, for example, when a mouse event is triggered on a touch screen by using the fingers instead of a mouse or when a mouse event, such as a mouse-moved event, is not triggered by a mouse button.

It is important to understand the difference between thegetButton() method and other methods, for example,isPrimaryButtonDown(), which returns the pressed state of buttons. ThegetButton() method returns the button that triggers the event. Not all mouse events are triggered by buttons.

For example, a mouse-move event is triggered when the mouse moves, not by pressing or releasing a button. If a button is not responsible for a mouse event, thegetButton() method returnsMouseButton.NONE. TheisPrimaryButtonDown() method returns true if the primary button is currently pressed, whether or not it triggered the event.

For example, when you press the primary button, the mouse-pressed event occurs. The
getButton() method will returnMouseButton.PRIMARY because this is the button that triggered the mouse-pressed event. TheisPrimaryButtonDown() method returns true because this button is pressed when the mouse-pressed event occurs. Suppose you keep the primary button pressed and you press the secondary button. Another mouse-pressed event occurs. However, this time, thegetButton() returnsMouseButton.SECONDARY and bothisPrimaryButtonDown() andisSecondaryButtonDown() methods return true, because both of these buttons are in the pressed state at the time of the second mouse-pressed event.

A pop-up menu, also known as a context, contextual, or shortcut menu, is a menu that gives a user a set of choices that are available in a specific context in an application. For example, when you click the right mouse button in a browser on the Windows platform, a pop-up menu is displayed. Different platforms trigger pop-up menu events differently upon use of a mouse or keyboard. On the Windows platform, typically it is a right-mouse click or Shift + F10 key press.

TheisPopupTrigger() method returns true if the mouse event is the pop-up menu trigger event for the platform. Otherwise, it returns false. If you perform an action based on the returned value of this method, you need to use it in both mouse-pressed and mouse-released events. Typically, when this method returns true, you let the system display the default pop-up menu.

1.4 State of Modifier Keys

A modifier key is used to change the normal behavior of other keys. Some examples of modifier keys are Alt, Shift, Ctrl, Meta, Caps Lock, and Num Lock. Not all platforms support all modifier keys. The Meta key is present on Mac, not on Windows. Some systems let you simulate the functionality of a modifier key even if the modifier key is physically not present, for example, you can use the Windows key on Windows to
work as the Meta key. TheMouseEvent method contains methods to report the pressed state of some of the modifier keys when the mouse event occurs.

1.5 Picking Mouse Events on Bounds

1.5.1 The Code

FxInputEventExample2.java

import javafx.application.Application;import javafx.event.ActionEvent;import javafx.event.EventHandler;import javafx.geometry.Insets;import javafx.scene.Group;import javafx.scene.Scene;import javafx.scene.control.CheckBox;import javafx.scene.control.TextArea;import javafx.scene.input.MouseEvent;import javafx.scene.layout.HBox;import javafx.scene.layout.VBox;import javafx.scene.paint.Color;import javafx.scene.shape.Circle;import javafx.scene.shape.Rectangle;import javafx.stage.Stage;public class FxInputEventExample2 extends Application{// Create the LoggingAreaprivate TextArea loggingArea = new TextArea("");// Create the CheckBoxprivate CheckBox checkbox = new CheckBox("Pick on Bounds");// Create the Circleprivate Circle circle = new Circle(50, 50, 50, Color.LIGHTGRAY);public static void main(String[] args) {Application.launch(args);}@Overridepublic void start(Stage stage) {// Create the RectangleRectangle rectangle = new Rectangle(100, 100);rectangle.setFill(Color.RED);// Create the GroupGroup group = new Group();// Add the children to the Groupgroup.getChildren().addAll(rectangle, circle);// Create the HBoxHBox hbox = new HBox();// Set Padding and Spacing for the HBoxhbox.setPadding(new Insets(20));hbox.setSpacing(20);// Add the children to the HBoxhbox.getChildren().addAll(group, checkbox);// Create the VBoxVBox root = new VBox();// Set Padding and Spacing for the VBoxroot.setPadding(new Insets(20));root.setSpacing(20);// Add the children to the VBoxroot.getChildren().addAll(hbox, loggingArea);// Add MOUSE_CLICKED event handlers to the Circlecircle.setOnMouseClicked(new EventHandler<MouseEvent>(){            public void handle(MouseEvent event)             {            handleMouseClicked(event);            }        });// Add MOUSE_CLICKED event handlers to the Rectanglerectangle.setOnMouseClicked(new EventHandler<MouseEvent>(){            public void handle(MouseEvent event)             {            handleMouseClicked(event);            }        });// Add an Action handler to the CheckBoxcheckbox.setOnAction(new EventHandler<ActionEvent>(){@Overridepublic void handle(ActionEvent event){handleActionEvent(event);}});// Set the Padding and Border for the VBoxroot.setStyle("-fx-padding: 10;" +"-fx-border-style: solid inside;" +"-fx-border-width: 2;" +"-fx-border-insets: 5;" +"-fx-border-radius: 5;" +"-fx-border-color: blue;");// Create the SceneScene scene = new Scene(root);// Add the Scene to the Stagestage.setScene(scene);// Set the Title of the Stagestage.setTitle("A Pick on Bounds Example");// Display the Stagestage.show();}public void handleMouseClicked(MouseEvent e) {// Get the source and type of the EventString target = e.getTarget().getClass().getSimpleName();String type = e.getEventType().getName();// Log the Informationsthis.loggingArea.appendText(type + " on " + target + "\n");}public void handleActionEvent(ActionEvent e) {if (checkbox.isSelected()) {circle.setPickOnBounds(true);} else {circle.setPickOnBounds(false);}}}

TheNode class has apickOnBounds property to control the way mouse events are picked (or generated) for a node. A node can have any geometric shape, whereas its bounds always define a rectangular area. If the property is set to true, the mouse events are generated for the node if the mouse is on the perimeter or inside of its bounds. If the property is set to false, which is the default value, mouse events are generated for the node if the mouse is on the perimeter or inside of its geometric shape. Some nodes, such as the Text node, have the default value for thepickOnBounds property set to true.

1.5.2 The GUI

The upper class displays a window as shown in the following image. The program adds aRectangle and aCircle to aGroup.

A JavaFX Pick on Bounds Example
A JavaFX Pick on Bounds Example

1.6 Mouse Transparency

1.6.1 The Code

FxInputEventExample3.java

import javafx.application.Application;import javafx.event.ActionEvent;import javafx.event.EventHandler;import javafx.geometry.Insets;import javafx.scene.Group;import javafx.scene.Scene;import javafx.scene.control.CheckBox;import javafx.scene.control.TextArea;import javafx.scene.input.MouseEvent;import javafx.scene.layout.HBox;import javafx.scene.layout.VBox;import javafx.scene.paint.Color;import javafx.scene.shape.Circle;import javafx.scene.shape.Rectangle;import javafx.stage.Stage;public class FxInputEventExample3 extends Application{// Create the LoggingAreaprivate TextArea loggingArea = new TextArea("");// Create the CheckBoxprivate CheckBox checkbox = new CheckBox("Mouse Transparent");// Create the Circleprivate Circle circle = new Circle(50, 50, 50, Color.LIGHTGRAY);public static void main(String[] args) {Application.launch(args);}@Overridepublic void start(Stage stage) {// Create the RectangleRectangle rectangle = new Rectangle(100, 100);rectangle.setFill(Color.RED);// Create the GroupGroup group = new Group();// Add the Children to the groupgroup.getChildren().addAll(rectangle, circle);// Create the HBoxHBox hbox = new HBox();// Set Padding and Spacing for the HBoxhbox.setPadding(new Insets(20));hbox.setSpacing(20);// Add the children to the HBoxhbox.getChildren().addAll(group, checkbox);// Create the VBoxVBox root = new VBox();// Set Padding and Spacing for the VBoxroot.setPadding(new Insets(20));root.setSpacing(20);// Add the children to the VBoxroot.getChildren().addAll(hbox, loggingArea);// Add MOUSE_CLICKED event handlers to the Circlecircle.setOnMouseClicked(new EventHandler<MouseEvent>(){            public void handle(MouseEvent event)             {            handleMouseClicked(event);            }        });// Add MOUSE_CLICKED event handlers to the Rectanglerectangle.setOnMouseClicked(new EventHandler<MouseEvent>(){            public void handle(MouseEvent event)             {            handleMouseClicked(event);            }        });// Add an Action Handler to the CheckBoxcheckbox.setOnAction(new EventHandler<ActionEvent>(){@Overridepublic void handle(ActionEvent event){handleActionEvent(event);}});// Set the Padding and Border for the VBoxroot.setStyle("-fx-padding: 10;" +"-fx-border-style: solid inside;" +"-fx-border-width: 2;" +"-fx-border-insets: 5;" +"-fx-border-radius: 5;" +"-fx-border-color: blue;");// Create the SceneScene scene = new Scene(root);// Add the Scene to the Stagestage.setScene(scene);// Set the Title of the Stagestage.setTitle("A Mouse Transparency Example");// Display the Stagestage.show();}public void handleMouseClicked(MouseEvent e) {// Get the source and type of the EventString target = e.getTarget().getClass().getSimpleName();String type = e.getEventType().getName();// Log the Informationsthis.loggingArea.appendText(type + " on " + target + "\n");}public void handleActionEvent(ActionEvent e) {if (checkbox.isSelected()) {circle.setMouseTransparent(true);} else {circle.setMouseTransparent(false);}}}

TheNode class has amouseTransparent property to control whether or not a node and its children receive mouse events. Contrast thepickOnBounds andmouseTransparent properties: The former determines the area of a node that generates mouse events, and the latter determines whether or not a node and its children generate mouse events, irrespective of the value of the former. The former affects only the node on which it is set. The latter affects the node on which it is set and all its children.

1.6.2 The GUI

The above program shows the effects of themouseTransparent property of aCircle.

A JavaFX Mouse Transparency Example
A JavaFX Mouse Transparency Example

1.7 Handling Mouse Entered and Exited Events

1.7.1 The Code

FxInputEventExample4.java

import javafx.application.Application;import javafx.event.EventHandler;import javafx.geometry.Insets;import javafx.scene.Scene;import javafx.scene.control.TextArea;import javafx.scene.input.MouseEvent;import javafx.scene.layout.VBox;import javafx.scene.paint.Color;import javafx.scene.shape.Circle;import javafx.stage.Stage;public class FxInputEventExample4 extends Application{// Create the LoggingAreaprivate TextArea loggingArea = new TextArea("");public static void main(String[] args) {Application.launch(args);}@Overridepublic void start(Stage stage) {// Create the CircleCircle circle = new Circle (50, 50, 50);circle.setFill(Color.GRAY);// Create the VBoxVBox root = new VBox();// Set Padding and Spacing for the VBoxroot.setPadding(new Insets(20));root.setSpacing(20);// Add the children to the VBoxroot.getChildren().addAll(circle, loggingArea);// Create a Mouse Event handlerEventHandler<MouseEvent> handler = new EventHandler<MouseEvent>(){            public void handle(MouseEvent event)             {            // Get the Type, Source and Target of the Event            String type = event.getEventType().getName();        String source = event.getSource().getClass().getSimpleName();        String target = event.getTarget().getClass().getSimpleName();                // Log the informations        loggingArea.appendText( "Type=" + type + ",, Source=" + source + "\n");            }        };// Add mouse-entered and mouse-exited event handlers to the VBoxroot.addEventHandler(MouseEvent.MOUSE_ENTERED, handler);root.addEventHandler(MouseEvent.MOUSE_EXITED, handler);// Add mouse-entered and mouse-exited event handlers to the Circlecircle.addEventHandler(MouseEvent.MOUSE_ENTERED, handler);circle.addEventHandler(MouseEvent.MOUSE_EXITED, handler);// Set the Padding and Border for the VBoxroot.setStyle("-fx-padding: 10;" +"-fx-border-style: solid inside;" +"-fx-border-width: 2;" +"-fx-border-insets: 5;" +"-fx-border-radius: 5;" +"-fx-border-color: blue;");// Create the SceneScene scene = new Scene(root);// Add the Scene to the Stagestage.setScene(scene);// Set the Title of the stagestage.setTitle("An Example of Mouse Entered and Exited Events");// Display the Stagestage.show();}}

FxInputEventExample5.java

import javafx.application.Application;import javafx.event.EventHandler;import javafx.geometry.Insets;import javafx.scene.Scene;import javafx.scene.control.CheckBox;import javafx.scene.control.TextArea;import javafx.scene.input.MouseEvent;import javafx.scene.layout.HBox;import javafx.scene.layout.VBox;import javafx.scene.paint.Color;import javafx.scene.shape.Circle;import javafx.stage.Stage;public class FxInputEventExample5 extends Application{// Create the LoggingAreaprivate TextArea loggingArea = new TextArea("");// Create the CheckBoxprivate CheckBox checkbox = new CheckBox("Consume Events");public static void main(String[] args) {Application.launch(args);}@Overridepublic void start(Stage stage) {// Create the CircleCircle circle = new Circle (50, 50, 50);circle.setFill(Color.GRAY);// Create the HBoxHBox hbox = new HBox();// Set Padding and Spacing for the HBoxhbox.setPadding(new Insets(20));hbox.setSpacing(20);// Add the children to the HBoxhbox.getChildren().addAll(circle, checkbox);// Create the VBoxVBox root = new VBox();// Set Padding and Spacing for the VBoxroot.setPadding(new Insets(20));root.setSpacing(20);// Add the children to the VBoxroot.getChildren().addAll(hbox, loggingArea);// Create mouse event handlersEventHandler<MouseEvent> circleHandler = new EventHandler<MouseEvent>(){            public void handle(MouseEvent event)             {            handleCircle(event);            }        };        EventHandler<MouseEvent> circleTargetHandler = new EventHandler<MouseEvent>(){            public void handle(MouseEvent event)             {            handleCircleTarget(event);            }        };EventHandler<MouseEvent> hBoxTargetHandler = new EventHandler<MouseEvent>(){            public void handle(MouseEvent event)             {            handleHBoxTarget(event);            }        };// Add mouse-entered-target and mouse-exited-target event handlers to VBoxroot.addEventFilter(MouseEvent.MOUSE_ENTERED_TARGET, hBoxTargetHandler);root.addEventFilter(MouseEvent.MOUSE_EXITED_TARGET, hBoxTargetHandler);// Add mouse-entered-target and mouse-exited-target event handlers to the Circlecircle.addEventHandler(MouseEvent.MOUSE_ENTERED_TARGET, circleTargetHandler);circle.addEventHandler(MouseEvent.MOUSE_EXITED_TARGET, circleTargetHandler);// Add mouse-entered and mouse-exited event handlers to the Circlecircle.addEventHandler(MouseEvent.MOUSE_ENTERED, circleHandler);circle.addEventHandler(MouseEvent.MOUSE_EXITED, circleHandler);// Set the Padding and Border for the VBoxroot.setStyle("-fx-padding: 10;" +"-fx-border-style: solid inside;" +"-fx-border-width: 2;" +"-fx-border-insets: 5;" +"-fx-border-radius: 5;" +"-fx-border-color: blue;");// Create the SceneScene scene = new Scene(root);// Add the Scene to the Stagestage.setScene(scene);// Set the Title of the Stagestage.setTitle("A Mouse Entered and Exited Target Events Example");// Display the Stagestage.show();}// Helper Methods for Event Handlingpublic void handleCircle(MouseEvent e) {print(e, "Circle Handler");}public void handleCircleTarget(MouseEvent e) {print(e, "Circle Target Handler");}public void handleHBoxTarget(MouseEvent e) {print(e, "HBox Target Filter");if (checkbox.isSelected()) {e.consume();this.loggingArea.appendText("HBox consumed the " + e.getEventType() + " event\n");}}public void print(MouseEvent e, String msg) {// Get the Type, Source and Target of the EventString type = e.getEventType().getName();String source = e.getSource().getClass().getSimpleName();String target = e.getTarget().getClass().getSimpleName();// Log the Informationsthis.loggingArea.appendText(msg + ": Type=" + type + ", "+ "Target=" + target + ", Source=" + source + "\n");}}

Four mouse event types deal with events when the mouse enters or exits a node:

  • MOUSE_ENTERED
  • MOUSE_EXITED
  • MOUSE_ENTERED_TARGET
  • MOUSE_EXITED_TARGET

You have two sets of event types for mouse-entered and mouse-exited events. One set contains two types calledMOUSE_ENTERED andMOUSE_EXITED and another set containsMOUSE_ENTERED_TARGET andMOUSE_EXITED_TARGET. They both have something in common, such as when they are triggered. They differ in their delivery mechanisms. I will discuss all of them this section.

When the mouse enters a node, aMOUSE_ENTERED event is generated. When the mouse leaves a node, aMOUSE_EXITED event is generated. These events do not go through the capture and bubbling phases. That is, they are delivered directly to the target node, not to any of its parent nodes.

TheMOUSE_ENTERED andMOUSE_EXITED event types provide the functionality needed in most cases. Sometimes you need these events to go through the normal capture and bubbling phases, so parent nodes can apply filters and provide default responses. TheMOUSE_ENTERED_TARGET andMOUSE_EXITED_TARGET event types provide these features. They participate in the event capture and bubbling phases.

TheMOUSE_ENTERED andMOUSE_EXITED event types are subtypes of theMOUSE_ENTERED_TARGET andMOUSE_EXITED_TARGET event types. A node interested in the mouse-entered event of its children should add event filters and handlers for theMOUSE_ENTERED_TARGET type. The child node can addMOUSE_ENTERED,MOUSE_ENTERED_TARGET, or both event filters and handlers.

When the mouse enters the child node, parent nodes receive theMOUSE_ENTERED_TARGET event. Before the event is delivered to the child node, which is the target node of the event, the event type is changed to theMOUSE_ENTERED type. Therefore, in the same event processing, the target node receives theMOUSE_ENTERED event, whereas all its parent nodes receive theMOUSE_ENTERED_TARGET event.

Because theMOUSE_ENTERED event type is a subtype of theMOUSE_ENTERED_TARGET type, either type of event handler on the target can handle this event. The same would apply to the mouse-exited event and its corresponding event types.

Sometimes, inside the parent event handler, it is necessary to distinguish the node that fires theMOUSE_ENTERED_TARGET event. A parent node receives this event when the mouse enters the parent node itself or any of its child nodes. You can check the target node reference, using thegetTarget() method of theEvent class, for equality with the reference of the parent node, inside the event filters and handlers, to know whether or not the event was fired by the parent.

1.7.2 The GUI

The following image shows how mouse-entered and mouse-exited events are delivered.

A JavaFX Mouse Entered and Exited Events Example

Want to master JavaFX ?
Subscribe to our newsletter and download theJavaFXProgramming Cookbookright now!
In order to get you prepared for your JavaFX development needs, we have compiled numerous recipes to help you kick-start your projects. Besides reading them online you may download the eBook in PDF format!

Thank you!

We will contact you soon.

The followin image shows a circle with gray fill inside anHBox.Event handlers for mouse-entered and mouse-exited events are added to theHBox and theCircle.

Run the program and move the mouse in and out of thecircle. When the mouse enters the white area in the window, itsMOUSE_ENTERED event is delivered to theHBox. When you move the mouse in and out of the circle, the output shows that theMOUSE_ENTERED andMOUSE_EXITED events are delivered only to theCircle, not to theHBox.

A JavaFX Mouse Entered and Exited Target Events Example
A JavaFX Mouse Entered and Exited Target Events Example

2. Handling Key Events

A key event is a type of input event that denotes the occurrence of a keystroke. It is delivered to the node that has focus. An instance of theKeyEvent class, which is declared in thejavafx.scene.input package, represents a key event. Key pressed, key released, and key typed are three types of key events.

The key-pressed and key-released events are lower-level events compared to the key-typed event. They occur with a key press and release, respectively, and depend of the platform and keyboard layout.

The key-typed event is a higher-level event. Generally, it does not depend on the platform and keyboard layout. It occurs when a Unicode character is typed. Typically, a key press generates a key-typed event. However, a key release may also generate a key-typed event. For example, when using the Alt key and number pad on Windows, a key-typed event is generated by the release of the Alt key, irrespective of the number of keystrokes entered on the number pad.

A key-typed event can also be generated by a series of key presses and releases. For example, the character A is entered by pressing Shift + A, which includes two key presses (Shift and A). In this case, two key presses generate one key-typed event. Not all key presses or releases generate key-typed events. For example, when you press a function key (F1, F2, etc.) or modifier keys (Shift, Ctrl, etc.), no Unicode character is entered, and hence, no key-typed event is generated.

TheKeyEvent class maintains three variables to describe the keys associated with the event: code, text, and character.

It is interesting to note that the return type of thegetCharacter() method isString, not char. The design is intentional. Unicode characters outside the basic multilingual plane cannot be represented in one character. Some devices may produce multiple characters using a single keystroke. The return type ofString for thegetCharacter() method covers these odd cases.

TheKeyEvent class containsisAltDown(),isControlDown(),isMetaDown(),isShiftDown(), andisShortcutDown() methods that let you check whether modifier keys are down when a key event occurs.

2.1 Handling Key-pressed and Key-released Events

2.1 Handling Key-pressed and Key-released Events

FxInputEventExample6.java

import javafx.application.Application;import javafx.event.EventHandler;import javafx.geometry.Insets;import javafx.scene.Scene;import javafx.scene.control.Label;import javafx.scene.control.TextArea;import javafx.scene.control.TextField;import javafx.scene.input.KeyCode;import javafx.scene.input.KeyEvent;import javafx.scene.layout.HBox;import javafx.scene.layout.VBox;import javafx.scene.text.Text;import javafx.stage.Stage;public class FxInputEventExample6 extends Application{// Create the LoggingAreaTextArea loggingArea = new TextArea("");public static void main(String[] args) {Application.launch(args);}@Overridepublic void start(Stage stage) {// Create the LabelLabel label = new Label("Name:");// Create the TextFieldTextField textfield = new TextField();// Create the HBoxHBox hbox = new HBox();// Set Padding and Spacing for the HBoxhbox.setPadding(new Insets(20));hbox.setSpacing(20);// Add the children to the HBoxhbox.getChildren().addAll(label, textfield);// Create the VBoxVBox root = new VBox();// Set Padding and Spacing for the VBoxroot.setPadding(new Insets(20));root.setSpacing(20);// Add the children to the VBoxroot.getChildren().addAll(hbox, loggingArea);// Add key pressed and released events to the TextFieldtextfield.setOnKeyPressed(new EventHandler<KeyEvent>() {            public void handle(final KeyEvent keyEvent)             {            handleEvent(keyEvent);            }        });textfield.setOnKeyReleased(new EventHandler<KeyEvent>() {            public void handle(final KeyEvent keyEvent)             {            handleEvent(keyEvent);            }        });// Set the Padding and Border for the VBoxroot.setStyle("-fx-padding: 10;" +"-fx-border-style: solid inside;" +"-fx-border-width: 2;" +"-fx-border-insets: 5;" +"-fx-border-radius: 5;" +"-fx-border-color: blue;");// Create the SceneScene scene = new Scene(root);// Add the Scene to the Stagestage.setScene(scene);// Set the Title of the Stagestage.setTitle("A Key Pressed and Released Events Example");// Display the Stagestage.show();}// Helper Methods for Event Handlingpublic void handleEvent(KeyEvent e) {// Get the Type of the EventString type = e.getEventType().getName();// Get the KeyCode of the EventKeyCode keyCode = e.getCode();// Log the InformationloggingArea.appendText(type + ": Key Code=" + keyCode.getName() +", Text=" + e.getText()+"\n");// Show the help window when the F1 key is pressedif (e.getEventType() == KeyEvent.KEY_PRESSED && e.getCode() == KeyCode.F1) {displayHelp();e.consume();}}public void displayHelp() {// Create the TextText text = new Text("Please enter a name.");// Create the HBoxHBox root = new HBox();// Set the Style of the HBoxroot.setStyle("-fx-background-color: yellow;");// Add the Children to the HBoxroot.getChildren().add(text);// Create the SceneScene scene = new Scene(root, 300, 200);// Create the StageStage helpStage = new Stage();// Add the Scene to the StagehelpStage.setScene(scene);// Set the Title of the StagehelpStage.setTitle("Help");// Display the StagehelpStage.show();}}

Key-pressed and key-released events are handled simply by adding the event filters and handlers to nodes for theKEY_PRESED andKEY_RELEASED event types. Typically you use these events to know which keys were pressed or released and to perform an action. For example, you can detect the F1 function key press and display a custom Help window for the node in focus.

The above code shows how to handle key-pressed and key-released events. It displays a Label and aTextField. When you run the program, theTextField has focus. Notice the following points when you use keystrokes while running this program:

Press and release some keys. Output will show the details of events as they occur. A key-released event does not occur for every key-pressed event.

The mapping between key-pressed and key-released events is not one-to-one. There may be no key-released event for a key-pressed event (refer to the next item). There may be one key-released event for several key-pressed events. This can happen when you keep a key pressed for a longer period. Sometimes you do it to type the same character multiple times. Press the A key and hold it for some time and then release it. This will generate several key-pressed events and only one key-released event.

Press the F1 key. It will display the Help window. Notice that pressing the F1 key does not generate an output for a key-released event, even after you release the key.

Can you think of the reason for this? On the key-pressed event, the Help window is displayed, which grabs the focus. TheTextField on the main window no longer has focus. Recall that the key events are delivered to the node that has focus, and only one node can have focus in a JavaFX application. Therefore, the key-released event is delivered to the Help window, not theTextField.

2.1.2 The GUI

The above program shows how to handle key-pressed and key-released events. It displays aLabel and aTextField. When you run the program, the TextField has focus.

A JavaFX Key Pressed and Released Events Example
A JavaFX Key Pressed and Released Events Example

2.2 Handling the Key-typed Event

2.2.1 The Code

FxInputEventExample7.java

import javafx.application.Application;import javafx.event.EventHandler;import javafx.geometry.Insets;import javafx.scene.Scene;import javafx.scene.control.Label;import javafx.scene.control.TextArea;import javafx.scene.control.TextField;import javafx.scene.input.KeyEvent;import javafx.scene.layout.HBox;import javafx.scene.layout.VBox;import javafx.stage.Stage;public class FxInputEventExample7 extends Application{// Create the LoggingAreaprivate TextArea loggingArea = new TextArea("");public static void main(String[] args) {Application.launch(args);}@Overridepublic void start(Stage stage) {// Create the LabelLabel label = new Label("Name:");// Create the TextFieldTextField textfield = new TextField();// Create the HBoxHBox hbox = new HBox();// Set Padding and Spacing for the HBoxhbox.setPadding(new Insets(20));hbox.setSpacing(20);// Add the children to the HBoxhbox.getChildren().addAll(label, textfield);// Create the VBoxVBox root = new VBox();// Set Padding and Spacing for the VBoxroot.setPadding(new Insets(20));root.setSpacing(20);// Add the children to the VBoxroot.getChildren().addAll(hbox, loggingArea);// Add key pressed and released events to the TextFieldtextfield.setOnKeyTyped(new EventHandler<KeyEvent>() {            public void handle(final KeyEvent keyEvent)             {            handleEvent(keyEvent);            }        });// Set the Padding and Border for the VBoxroot.setStyle("-fx-padding: 10;" +"-fx-border-style: solid inside;" +"-fx-border-width: 2;" +"-fx-border-insets: 5;" +"-fx-border-radius: 5;" +"-fx-border-color: blue;");// Create the SceneScene scene = new Scene(root);// Add the Scene to the Stagestage.setScene(scene);// Set the Title of the Stagestage.setTitle("A Key Typed Event Example");// Display the Stagestage.show();}public void handleEvent(KeyEvent e) {// Consume the event if it is not a letterString str = e.getCharacter();int len = str.length();for(int i = 0; i < len; i++) {Character c = str.charAt(i);if (!Character.isLetter(c)) {e.consume();}}// Print the details if it is not consumedif (!e.isConsumed()) {String type = e.getEventType().getName();this.loggingArea.appendText(type + ": Character=" + e.getCharacter() + "\n");}}}

The typical use of the key-typed event is to detect specific keystrokes to prevent some characters from being entered. For example, you may allow users to only enter letters in a name field. You can do so by consuming all key-typed events for the field associated with all nonletters.

The above code snippet shows a Label and aTextField. It adds a key-typed event handler to theTextField, which consumes the event if the character typed is not a letter. Otherwise, it prints the character typed on the standard output. Run the program. You should be able to enter letters in theTextField. When you press any nonletter keys, for example, 1, 2, 3, nothing happens.

This example is not a correct solution to stop users from entering nonletter characters. For example, users can still paste nonletters using the context menu (right-click on Windows) or using the keyboard shortcut Ctrl + V. The correct solution lies in detecting and handling the event on the TextField that is generated, irrespective of the method used. For now, this example serves the purpose of showing how to use key-typed events.

2.2.2 The GUI

The following image shows the result of the above program.

A JavaFX Key Typed Event Example
A JavaFX Key Typed Event Example

3. Handling Window Events

3.1 The Code

FxInputEventExample8.java

import javafx.application.Application;import javafx.event.ActionEvent;import javafx.event.EventHandler;import javafx.event.EventType;import javafx.geometry.Insets;import javafx.scene.Scene;import javafx.scene.control.Button;import javafx.scene.control.CheckBox;import javafx.scene.control.TextArea;import javafx.scene.layout.HBox;import javafx.scene.layout.VBox;import javafx.stage.Stage;import javafx.stage.WindowEvent;public class FxInputEventExample8 extends Application{// Create the LoggingAreaprivate TextArea loggingArea = new TextArea("");// Create the CheckBoxprivate CheckBox checkbox = new CheckBox("Can Close Window");public static void main(String[] args) {Application.launch(args);}public void start(final Stage stage) {// Create the ButtonsButton closeButton = new Button("Close");Button hideButton = new Button("Hide");// Add the Event Handlers to the ButtonscloseButton.setOnAction(new EventHandler<ActionEvent>() {            public void handle(ActionEvent event)             {            stage.close();            }        });hideButton.setOnAction(new EventHandler<ActionEvent>() {            public void handle(ActionEvent event)             {            showDialog(stage);             stage.hide();            }        });// Create the HBoxHBox hbox = new HBox();// Set Padding and Spacing for the HBoxhbox.setPadding(new Insets(20));hbox.setSpacing(20);// Add the children to the HBoxhbox.getChildren().addAll(checkbox, closeButton, hideButton);// Create the VBoxVBox root = new VBox();// Set Padding and Spacing for the VBoxroot.setPadding(new Insets(20));root.setSpacing(20);// Add the children to the VBoxroot.getChildren().addAll(hbox, loggingArea);// Add window event handlers to the Stagestage.setOnShowing(new EventHandler<WindowEvent>() {            public void handle(WindowEvent event)             {            handleEvent(event);            }        });stage.setOnShown(new EventHandler<WindowEvent>() {            public void handle(WindowEvent event)             {            handleEvent(event);            }        });stage.setOnHiding(new EventHandler<WindowEvent>() {            public void handle(WindowEvent event)             {            handleEvent(event);            }        });stage.setOnHidden(new EventHandler<WindowEvent>() {            public void handle(WindowEvent event)             {            handleEvent(event);            }        });stage.setOnCloseRequest(new EventHandler<WindowEvent>() {            public void handle(WindowEvent event)             {            handleEvent(event);            }        });// Set the Padding and Border for the VBoxroot.setStyle("-fx-padding: 10;" +"-fx-border-style: solid inside;" +"-fx-border-width: 2;" +"-fx-border-insets: 5;" +"-fx-border-radius: 5;" +"-fx-border-color: blue;");// Create the SceneScene scene = new Scene(root);// Add the Scene to the Stagestage.setScene(scene);// Set the Title of the Stagestage.setTitle("A Window Events Example");// Display the Stagestage.show();}public void handleEvent(WindowEvent e) {// Consume the event if the CheckBox is not selected// thus preventing the user from closing the windowEventType<WindowEvent> type = (EventType<WindowEvent>) e.getEventType();if (type == WindowEvent.WINDOW_CLOSE_REQUEST && !checkbox.isSelected()) {e.consume();}// Log the Informationthis.loggingArea.appendText(type + ": Consumed=" + e.isConsumed() + "\n");}public void showDialog(final Stage mainWindow) {// Create the Stagefinal Stage popup = new Stage();// CReate the ButtonButton closeButton = new Button("Click to Show Main Window");// Add the Event Handler to the ButtoncloseButton.setOnAction(new EventHandler<ActionEvent>() {            public void handle(ActionEvent event)             {            popup.close();            mainWindow.show();            }        });// Create the HBoxHBox root = new HBox();// Set Padding and Spacing for the HBoxroot.setPadding(new Insets(20));root.setSpacing(20);// Add the children to the HBoxroot.getChildren().addAll(closeButton);// Create the SceneScene scene = new Scene(root);// Add the Scene to the Stagepopup.setScene(scene);// Set the Title of the STagepopup.setTitle("Popup");// Display the Stagepopup.show();}}

A window event occurs when a window is shown, hidden, or closed. An instance of theWindowEvent class in thejavafx.stage package represents a window event.

The window-showing and window-shown events are straightforward. They occur just before and after the window is shown. Event handlers for the window-showing event should have time-consuming logic, as it will delay showing the window to the user, and hence, degrading the user experience.

Initializing some window-level variables is a good example of the kind of code you need to write in this event. Typically, the window-shown event sets the starting direction for the user, for example, setting focus to the first editable field on the window, showing alerts to the user about the tasks that need his attention, among others.

The window-hiding and window-hidden events are counterparts of the window-showing and
window-shown events. They occur just before and after the window is hidden. The window-close-request event occurs when there is an external request to close the window. Using
the Close menu from the context menu or the Close icon in the window title bar or pressing Alt + F4 key combination on Windows is considered an external request to close the window. Note that closing a window programmatically, for example, using theclose() method of the Stage class orPlatform.exit() method, is not considered an external request. If the window-close-request event is consumed, the window is not closed.

The program in Listing 9-13 shows how to use all window events. You may get a different output than that shown below the code. It adds a check box and two buttons to the primary stage. If the check box is unselected, external requests to close the window are consumed, thus preventing the window from closing.

The Close button closes the window. The Hide button hides the primary window and opens a new window, so the user can show the primary window again. The program adds event handlers to the primary stage for window event types.

When theshow() method on the stage is called, the window-showing and window-shown events are generated. When you click the Hide button, the window-hiding and window-hidden events are generated.

When you click the button on the pop-up window to show the primary window, the window-showing and window-shown events are generated again. Try clicking the Close icon on the title bar to generate the window-close-request event. If the Can Close Window check box is not selected, the window is not closed. When you use the Close button to close the window, the window-hiding and window-hidden events are generated, but not the window-close-request event, as it is not an external request to close the window.

3.2 The GUI

The following image shows the result of the execution of the above code.

A JavaFX Window Event Example
A JavaFX Window Event Example

4. Download Source Code

This was an example ofjavafx.scene.input

Download
You can download the full source code of this example here:JavaFxInputEventExample.zip
Do you want to know how to develop your skillset to become aJava Rockstar?
Subscribe to our newsletter to start Rockingright now!
To get you started we give you our best selling eBooks forFREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to theTerms andPrivacy Policy

Thank you!

We will contact you soon.

Photo of Andreas PomarolliAndreas PomarolliSeptember 30th, 2016Last Updated: April 24th, 2019
1 1,111 26 minutes read
Photo of Andreas Pomarolli

Andreas Pomarolli

Andreas has graduated from Computer Science and Bioinformatics at the University of Linz. During his studies he has been involved with a large number of research projects ranging from software engineering to data engineering and at least web engineering. His scientific focus includes the areas of software engineering, data engineering, web engineering and project management. He currently works as a software engineer in the IT sector where he is mainly involved with projects based on Java, Databases and Web Technologies.

Related Articles

The JavaFX Print API

July 8th, 2016

JavaFX WebView Example

October 13th, 2016

JavaFX Canvas Example

May 18th, 2016

JavaFX CSS Tutorial

May 5th, 2016

The JavaFX Media API

August 8th, 2016
Subscribe
Notify of
guest
I agree to theTerms andPrivacy Policy
The comment form collects your name, email and content to allow us keep track of the comments placed on the website. Please read and accept our website Terms and Privacy Policy to post a comment.

I agree to theTerms andPrivacy Policy
The comment form collects your name, email and content to allow us keep track of the comments placed on the website. Please read and accept our website Terms and Privacy Policy to post a comment.