CSE1007 - JAVA PROGRAMMING
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.
Things you need to know
• What kinds of events are there?
• What user (or software) action causes what event?
• How do you write an event handler?
• How do you add event handler to a component?
Event Handling
Event Handling
                Event Handling
Sources of Event:
            Event Handling
Event Handler:
             Event Handling
• Ex: Enter or Button Click
             Event Handling
Add Event Handler
               Event Handling
• Two Ways to Add Event Handler
              Event Handling
• Re-use event handlers
Define an EventHandler
                   Example
Working with TextField, Label and Button
• Two numbers are given as inputs in two
  textfields and upon clicking ‘add’, they both are
  added and the sum is displayed in textfield 3.
  Upon clicking ‘multiply’, their product is
  displayed
Example 1
      Example 2
Working with CheckBox
       Example3
Working with Radiobuttons
                Example 4
Working with Mouse: Moving the ball based on
             mouse movements
     Example 5
Working with MenuBar
Example 6 - Working with Scrollbar
                      Dialog
• Alert Dialog
   – CONFIRMATION alert
   – WARNING alert
   – ERROR alert
   – INFORMATION alert
   – NONE alert
• Choice Dialog
• Text Input Dialog
                JavaFX Alert Dialog
• Alert is a part of JavaFX and it is a subclass of Dialog class. Alerts are
  some predefined dialogs that are used to show some information to the
  user.
Alerts are basically of specific alert types:
• CONFIRMATION alert : The CONFIRMATION alert type configures
   the Alert dialog to appear in a way that suggests the content of the
   dialog is seeking confirmation from the user.
• WARNING alert : The WARNING alert type configures the Alert
   dialog to appear in a way that suggests the content of the dialog is
   warning the user about some fact or action.
• INFORMATION alert : The INFORMATION alert type configures
   the Alert dialog to appear in a way that suggests the content of the
   dialog is informing the user of a piece of information.
• ERROR alert : The ERROR alert type configures the Alert dialog to
   appear in a way that suggests that something has gone wrong
• NONE alert : The NONE alert type has the effect of not setting any
   default properties in the Alert.
JavaFX Alert Dialog
JavaFX Alert Dialog
            JavaFX Alert Dialog
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
Alert a = new Alert(AlertType.INFORMATION);
a.setTitle(titleTxt);
a.setHeaderText("Information Alert");
String s ="This is an example of JavaFX Dialogs... ";
a.setContentText(s);
a.show();
               Choice Dialog
• ChoiceDialog is a dialog that gives the user a set
  of options from which the user can select at most
  one option
                    Choice 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
Scene sc = new Scene(root, 200, 200);
 // add a change listener
group.selectedToggleProperty().addListener(new ChangeListener<Toggle>()
 {
      public void changed(ObservableValue<? extends Toggle> ob, Toggle o, Toggle n)
         {
            RadioButton rb = (RadioButton)group.getSelectedToggle();
            if (rb != null)
           {
               String s = rb.getText();
               // change the label
               l.setText(s + "selected");
            }
         }
     });
     // set the scene
     s.setScene(sc);
     s.show();