Convenience Methods
JavaFX provides the convenience methods which can be used to handle events within our JavaFX application. They provide an easy way to create and register event handlers to respond to KeyEvent, MouseEvent, Action Event, Drag & Drop Events and many more.
Node class contains various Event Handler properties which can be set to the User defined Event Handlers using the setter methods defined in the class.
Setting, EventHandler properties of the Node class, to the user defined event handlers, automatically registers the handlers to receive the corresponding event types.
The EventHandler properties along with their setter methods (convenience methods) are described in the following table.
EventHandler Property | Description | Setter Methods |
---|---|---|
onContextMenuRequested | This is of the type EventHandler of ContextMenuEvent. This is assigned to a function which is to be called when the context menu is requested on the node. | setOnContextMenuRequested(EventHandler value ) |
onDragDetected | This is of the type EventHandler of MouseEvent. This indicates a function which is to be called when the drag gesture is detected. | setOnDragDetected(EventHandler value ) |
onDragDone | This is of the type EventHandler of DragEvent. | setOnDragDone(EventHandler value ) |
onDragDropped | This is of the type EventHandler of DragEvent. This is assigned to a function which is to be called when the mouse is released during a drag and drop operation. | setOnDragDropped(EventHandler value ) |
onDragEntered | This is of the type EventHandler of DragEvent. This is assigned to a function which is to be called when the drag gesture enters the node. | setOnDragEntered(EventHandler value ) |
onDragExited | This is of the type EventHandler of DragEvent. This is assigned to a function which is to be called when the drag gesture exits the node. | setOnDragExited(EventHandler value ) |
onDragOver | This is of the type EventHandler of DragEvent.This is assigned to a function which is to be called when the drag gesture progresses within the node. | setOnDragOver(EventHandler value ) |
onInputMethodTextChanged | This is of the type EventHandler of InputMethodEvent. This is assigned to a function which is to be called when the Node has focus and the input method text has changed. | setOnInputMethodTextChanged(EventHandler value ) |
onKeyPressed | This is of the type EventHandler of KeyEvent. This is assigned to a function which is to be called when the Node has focus and key is pressed. | setOnKeyPressed(EventHandler value ) |
onKeyReleased | This is of the type EventHandler of KeyEvent. This is assigned to a function which is to be called when the Node has focus and key is released. | setOnKeyReleased(EventHandler value ) |
onKeyTyped | This is of the type EventHandler of KeyEvent.This is assigned to a function which is to be called when the Node has focus and key is typed. | setOnKeyTyped(EventHandler value ) |
onMouseClicked | This is of the type EventHandler of MouseEvent. This is assigned to a function which is to be called when the mouse button is clicked on the node. | setOnMouseClicked(EventHandler value ) |
onMouseDragEntered | This is of the type EventHandler of MouseDragEvent. This is assigned to a function which is to be called when a full press drag release gesture enters the node. | setOnMouseDragEntered(EventHandler value ) |
onMouseDragExited | This is of the type EventHandler of MouseDragEvent.This is assigned to a function which is to be called when a full press drag release gesture exits the node. | setOnMouseDragExited(EventHandler value ) |
onMouseDragged | This is of the type EventHandler of MouseDragEvent. This is assigned to a function which is to be called when the mouse button is pressed and dragged on the node. | setOnMouseDragged(EventHandler value ) |
onMouseDragOver | This is of the type EventHandler of MouseDragEvent. This is assigned to a function which is to be called when a full press drag release gesture progresses within the node. | setOnMouseDragOver(EventHandler value ) |
onMouseDragReleased | This is of the type EventHandler of MouseDragEvent. This is assigned to a function which is to be called when a full press drag release gesture ends within the node. | setOnMouseDragReleased(EventHandler value ) |
onMouseEntered | This is of the type EventHandler of MouseEvent. This is assigned to a function which is to be called when the mouse enters the node. | setOnMouseEntered(EventHandler value ) |
onMouseExited | This is of the type EventHandler of MouseEvent. This is assigned to a function which is to be called when the mouse exits the node. | setOnMouseExited(EventHandler value ) |
onMouseMoved | This is of the type EventHandler of MouseEvent. This is assigned to a function which is to be called when the mouse moves within the node and no button has been pushed. | setOnMouseMoved(EventHandler value ) |
onMousePressed | This is of the type EventHandler of MouseEvent. This is assigned to a function which is to be called when the mouse button is pressed on the node. | setOnMousePressed(EventHandler value ) |
onMouseReleased | This is of the type EventHandler of MouseEvent. This is assigned to a function which is to be called when the mouse button is released on the node. | setOnMouseReleased(EventHandler value ) |
onRotate | This is of the type EventHandler of RotateEvent. This is assigned to a function which is to be called when the rotation action is performed on the node. | setOnRotate(EventHandler value ) |
onRotationFinished | This is of the type EventHandler of RotateEvent. This is assigned to a function which is to be called when the rotation gesture ends. | setOnRotationFinished(EventHandler value ) |
onRotationStarted | This is of the type EventHandler of RotateEvent. This is assigned to a function which is to be called when the rotation gesture is first detected. | setOnRotationStarted(EventHandler value ) |
onScrollFinished | This is of the type EventHandler of ScrollEvent. This is assigned to a function which is to be called when the scroll gesture ends. | setOnScrollFinished(EventHandler value ) |
onScroll | This is of the type EventHandler of ScrollEvent. This is assigned to a function which is to be called when the scroll action is performed. | setOnScroll(EventHandler value ) |
onScrollStarted | This is of the type EventHandler of ScrollEvent. This is assigned to a function which is to be called when the scrolling gesture is detected. | setOnScrollStarted(EventHandler value ) |
onSwipeDown | This is of the type EventHandler of SwipeEvent. This is assigned to a function which is to be called when the downwards swipe happens on the node. | setOnSwipeDown(EventHandler value ) |
onSwipeUP | This is of the type EventHandler of SwipeEvent. This is assigned to a function which is to be called when the upwards swipe happens on the node. | setOnSwipeUP(EventHandler value ) |
onSwipeLeft | This is of the type EventHandler of SwipeEvent. This is assigned to a function which is to be called when the leftwards swipe happens on the node. | setOnSwipeLeft(EventHandler value ) |
onSwipeRight | This is of the type EventHandler of SwipeEvent. This is assigned to a function which is to be called when the Rightwards swipe happens on the node. | setOnSwipeRight(EventHandler value ) |
onTouchMoved | This is of the type EventHandler of TouchEvent. This is assigned to a function which is to be called when the touch point is moved on the node. | setOnTouchMoved(EventHandler value ) |
onTouchReleased | This is of the type EventHandler of TouchEvent. This is assigned to a function which is to be called when the touch point is released on the node. | setOnTouchReleased(EventHandler value ) |
onTouchStationary | This is of the type EventHandler of TouchEvent. This is assigned to a function which is to be called when the touch point is pressed and stays still | setOnTouchStationary(EventHandler value ) |
onZoomFinished | This is of the type EventHandler of ZoomEvent. This is assigned to a function which is to be called when the zooming gesture ends. | setOnZoomFinished(EventHandler value ) |
onZoom | This is of the type EventHandler of ZoomEvent. This is assigned to a function which is to be called when the zooming gesture is performed. | setOnZoom(EventHandler value ) |
onZoomStarted | This is of the type EventHandler of ZoomEvent. This is assigned to a function which is to be called when the zooming gesture is detected. | setOnZoomStarted(EventHandler value ) |
Convenience methods for the event handlers registration has the format like
where the Event type is the type of the event that is to be handled through the defined functions, for example, setOnMouseMoved() will be the convenience method to register the event handler for the event Mouse_Moved.
setOnAction() Example for a Button Action
In the following example, setOnAction() method is illustrated. The EventHandler registered with the setOnAction() method is called when the Play button is clicked and it is set to rotate the rectangle on the screen.
Pause button is also registered with the EventHandler which is set to stop the rotation of rectangle.
setOnKeyEvent() Example for a Key Event
The setOnKeyEvent() method can be used for registering the Event Handler logic for the key event generated on a node. In the following example, two text fields are created as the node, the key pressed in the first text field is set as the text in the second text field.