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 PropertyDescriptionSetter Methods
onContextMenuRequestedThis 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 )
onDragDetectedThis 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 )
onDragDoneThis is of the type EventHandler of DragEvent.setOnDragDone(EventHandler value )
onDragDroppedThis 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 )
onDragEnteredThis 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 )
onDragExitedThis 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 )
onDragOverThis 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 )
onInputMethodTextChangedThis 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 )
onKeyPressedThis 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 )
onKeyReleasedThis 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 )
onKeyTypedThis 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 )
onMouseClickedThis 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 )
onMouseDragEnteredThis 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 )
onMouseDragExitedThis 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 )
onMouseDraggedThis 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 )
onMouseDragOverThis 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 )
onMouseDragReleasedThis 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 )
onMouseEnteredThis 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 )
onMouseExitedThis 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 )
onMouseMovedThis 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 )
onMousePressedThis 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 )
onMouseReleasedThis 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 )
onRotateThis 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 )
onRotationFinishedThis 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 )
onRotationStartedThis 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 )
onScrollFinishedThis 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 )
onScrollThis 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 )
onScrollStartedThis 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 )
onSwipeDownThis 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 )
onSwipeUPThis 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 )
onSwipeLeftThis 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 )
onSwipeRightThis 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 )
onTouchMovedThis 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 )
onTouchReleasedThis 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 )
onTouchStationaryThis 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 stillsetOnTouchStationary(EventHandler value )
onZoomFinishedThis 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 )
onZoomThis 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 )
onZoomStartedThis 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.


JavaFX Convenience Methods




Latest Courses