JavaFX Event

0

JavaFX Event tutorial:

An event is a thing that happens or takes place, for example, clicks a button, moves a mouse and presses a key etc.

Events are generated by user or application.

Different Events in JavaFX :

An event consists following properties:

  • Event type
  • Event Source
  • Event Target

Event Types :

Event type is type of event occurred.

In JavaFX there are many types of events like
ActionEvent and InputEvent etc.

Below figure shows a hierarchy of Event Type
(Source of figure is oracle docs)

User Action Event Type Class
Key on the keyboard is pressed. KeyEvent Node, Scene
Mouse is moved or a button on the mouse is pressed. MouseEvent Node, Scene
Full mouse press-drag-release action is performed. MouseDragEvent Node, Scene
Input from an alternate method for entering characters (typically for a foreign language) is generated, changed, removed, or committed. InputMethodEvent Node, Scene
Platform-supported drag and drop action is performed. DragEvent Node, Scene
Object is scrolled. ScrollEvent Node, Scene
Rotation gesture is performed on an object RotateEvent Node, Scene
Swipe gesture is performed on an object SwipeEvent Node, Scene
An object is touched TouchEvent Node, Scene
Zoom gesture is performed on an object ZoomEvent Node, Scene
Context menu is requested ContextMenuEvent Node, Scene
Button is pressed, combo box is shown or hidden, or a menu item is selected. ActionEvent ButtonBase, ComboBoxBase, ContextMenu, MenuItem, TextField
Item in a list, table, or tree is edited.
  • ListView.EditEvent
  • TableColumn.CellEditEvent
  • TreeView.EditEvent
  • ListView
  • TableColumn
  • TreeView
Media player encounters an error. MediaErrorEvent MediaView
Menu is either shown or hidden. Event Menu
Popup window is hidden. Event PopupWindow
Tab is selected or closed. Event Tab
Window is closed, shown, or hidden. WindowEvent Window

Here InputEvent.ANY means there is more sub event of this type and we can use all of them or a specific subtype of this InputEvent types.

Constructors :

  • Event(EventType<? extends Event> eventType)
    Construct a new Event with the specified event type.
  • Event(Object source, EventTarget target, EventType<? extends Event> eventType)
    Construct a new Event with the specified event source, target and type.
setOnEvent-type() :

setOnEvent-type() method is used for registering event handlers.

setOnEvent-type(EventHandler<? super event-class> value)

Where :

Event-type is the type of event which is processed by the handler. For example setOnAction for Action events or setOnMouseClicked for MOUSE_CLICKED events.

event-class is a class which defines the event type. For example, WindowEvent for events related to window display or MouseEvent for events related to mouse input.

The string <? super event-class> Means method accept an event handler for event-class or for super class of its. For example, an event handler for Event could be used when the event is either a InputEvent or a WindowEvent.

Question : When we use which type of Event ?Answer :

To understood the concept of using the proper event-type for an event we should see the diagram below carefully(source of the diagram is oracle).

For example it is clear that WindowEvent which is used when window is closed, shown or hidden.

Example using javafx event:


package javafxtuts;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;

public class Javafxtuts extends Application {

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

  @Override
  public void start(Stage stage) {
      stage.setTitle("Javafxtuts.com");
      Label label =new Label();
	//creating a new button.
      Button button = new Button();
      button.setText("My Button");
	//register a handler for button.
      button.setOnAction(new EventHandler() {
      @Override
      public void handle(ActionEvent event) {
        label.setText("You clicked My Button.");
      }
    });
      BorderPane root = new BorderPane();
      root.setTop(button);
      root.setCenter(label);
        Scene scene = new Scene(root, 300, 200);
        stage.setScene(scene);
        stage.show();
  }
}

Output :

Explanation :
  • setOnAction() method is used to register an event handler.
  • And handle method of the event handler is called when a user clicks on My Button. Which takes event as an argument.
    public void handle(ActionEvent event) {}
  • And handle method set the text of the label to “You clicked My Button.” . as
    label.setText(“You clicked My Button.”);
Share.

Leave A Reply