quinta-feira, 23 de janeiro de 2014

A generic handler for all input events in JavaFX 2

The JavaFX API is really complete and sometimes I find new possibilities with it. This is a small blog post to describe what I recently did with the event handling part of the JavaFX API.

Update: Jonathan Giles pointed that we can add a handler to all kind of events with one single line instead call setOn* multiple times: lbl.addEventHandler(MouseEvent.ANY, eventHandler)

One handler to all Events
I'm creating a sample application for my portuguese blog about JavaFX basics and I was exploring the Event class to find the best way to create an event handler to demonstrate all Node's possible event input handlers.(or the most famous one)
I found that the event object has an attribute of type EventType and all the events implementations contains a String representation of that event. It can be done checking, for example, the MouseEvent class source code.
With this I created only one class to handle all the events on my sample program. I think it might be useful at certain cases so I decided to share the following Java code that declares only one handler to all the possible events generated in one node!
Notice that this might not be needed at all when Java 8 comes out, it's so easy to write a handler using Lambda Expressions...

Anyway, here's the sample code:


package main;

import javafx.application.Application;
import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

public class Main extends Application { 

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

 @Override
 public void start(Stage palco) throws Exception { 
  StackPane root = new StackPane(); 
  Label lbl = new Label(); 
  lbl.setText("Just a Test..");
  lbl.setFocusTraversable(true);
  root.getChildren().add(lbl); 
  Scene cena = new Scene(root, 250, 100); 
  palco.setTitle("Generic Handler"); 
  palco.setScene(cena); 
  palco.show(); 
  
  EventHandler eventHandler = new EventHandler() {
   @Override
   public void handle(Event evt) {
    String text = "";
    String eventType = evt.getEventType().toString();
    switch (eventType) {
     case "MOUSE_CLICKED":
      text = "Mouse Clicked";
      break; 
     case "MOUSE_ENTERED":
      text = "Mouse entedered";
      break; 
     case "MOUSE_EXITED":
      text = "Mouse exited";
      break;
     case "MOUSE_DRAGGED":
      text = "Mouse dragged";
      break; 
     case "MOUSE_MOVED":
      text = "Mouse mouved";
      break;
     case "MOUSE_RELEASED":
      text = "Mouse releasead";
      break;
     case "MOUSE_PRESSED":
      text = "Mouse pressed";
      break; 
     case "KEY_PRESSED":
      text = "Key pressed";
      break;   
      // could have others events as well...
     default:
      text = "Evento desconhecido: "+eventType;
      break;
    }
    System.out.println(text);    
   }
  };  
  // setting the handler to the events
  lbl.setOnMouseClicked(eventHandler);  
  lbl.setOnMouseEntered(eventHandler);
  lbl.setOnMouseExited(eventHandler);
  lbl.setOnMouseDragged(eventHandler);
  lbl.setOnMouseMoved(eventHandler);
  lbl.setOnMousePressed(eventHandler);
  lbl.setOnMouseReleased(eventHandler);
  lbl.setOnKeyPressed(eventHandler);  
 }
}

Nenhum comentário:

Postar um comentário