Professional Documents
Culture Documents
MODULE-5
Event Handling
Event Handling
• In JavaFX, we can develop GUI applications, web
applications and graphical applications.
• In such applications, whenever a user interacts with the
application (nodes), an event is said to have been occurred.
• For example, clicking on a button, moving the mouse,
entering a character through keyboard, selecting an item
from list, scrolling the page are the activities that causes an
event to happen.
• JavaFX provides support to handle a wide varieties of
events.
• The class named Event of the package javafx.event is the
base class for an event.
• An instance of any of its subclass is an event.
Event Handling
Some of the events are listed below.
• Mouse Event − This is an input event that occurs when a mouse is
clicked. It is represented by the class named MouseEvent. It
includes actions like mouse clicked, mouse pressed, mouse
released, mouse moved, mouse entered target, mouse exited target,
etc.
• Key Event − This is an input event that indicates the key stroke
occurred on a node. It is represented by the class named KeyEvent.
This event includes actions like key pressed, key released and key
typed.
• Drag Event − This is an input event which occurs when the mouse
is dragged. It is represented by the class named DragEvent. It
includes actions like drag entered, drag dropped, drag entered
target, drag exited target, drag over, etc.
Event Handling
• Window Event − This is an event related to window
showing/hiding actions. It is represented by the class named
WindowEvent. It includes actions like window hiding,
window shown, window hidden, window showing, etc.
Event Handling
• Event Handling is the mechanism that controls the event and
decides what should happen, if an event occurs. This
mechanism has the code which is known as an event handler
that is executed when an event occurs.
• JavaFX provides handlers and filters to handle events.
• In JavaFX every event has −
– Target − The node on which an event occurred. A target
can be a window, scene, and a node.
– Source − The source from which the event is generated
will be the source of the event. In the above scenario,
mouse is the source of the event.
– Type − Type of the occurred event; in case of mouse event
mouse pressed, mouse released are the type of events.
Event Handling
• Graphics applications use events.
• An event dispatcher receives events and notifies
interested objects
Event Handling
Event Handling
Responding to Behavior
Your application must do something when an event
occurs.
Method Explanation
getAlertType() Get a specified alert type
Set a specified alert type for the
setAlertType(Alert.AlertType a)
alert
Sets the context text for the
setContentText(String s)
alert
Returns the content text for the
getContentText()
alert.
JavaFX Alert Dialog
Method Explanation
Returns all the available items of the
getItems()
choice box
Returns the selected item of the
getSelectedItem()
dialog
setSelectedItem(T item) Sets the selected item of the dialog
setContentText(String s) Sets the content text of the dialog
setHeaderText(String s) Sets the header textof the dialog
Choice Dialog
JavaFX TextInputDialog
• TextInputDialog is a dialog that allows the
user to enter a text, and the dialog contains a
header text, a TextField and confirmation
buttons.
JavaFX TextInputDialog
Examples
Event Handling - Example
GridPane grid = new GridPane();
grid.setAlignment(Pos.CENTER);
grid.setHgap(10);
grid.setVgap(10);
grid.setPadding(new Insets(25, 25, 25, 25));
Scene scene = new Scene(grid, 300, 275);
primaryStage.setScene(scene);
Text scenetitle = new Text("Welcome");
scenetitle.setFont(Font.font("Tahoma", FontWeight.NORMAL, 20));
grid.add(scenetitle, 0, 0, 2, 1);//row,column,columnspan, rowspan
Label userName = new Label("User Name:");
grid.add(userName, 0, 1); final Text actiontarget = new Text();
TextField userTextField = new TextField(); grid.add(actiontarget, 1, 6);
grid.add(userTextField, 1, 1); btn.setOnAction(new EventHandler<ActionEvent>()
Label pw = new Label("Password:"); {
grid.add(pw, 0, 2); public void handle(ActionEvent e)
PasswordField pwBox = new PasswordField(); {
grid.add(pwBox, 1, 2); actiontarget.setFill(Color.FIREBRICK);
Button btn = new Button("Sign in"); actiontarget.setText("Sign in button pressed");
HBox hbBtn = new HBox(10); }
hbBtn.setAlignment(Pos.BOTTOM_RIGHT); });
hbBtn.getChildren().add(btn);
grid.add(hbBtn, 1, 4);
Display on click of a Radio Button