1  import javafx.application.Application;
  2  import javafx.event.ActionEvent;
  3  import javafx.event.EventHandler;
  4  import javafx.geometry.Pos;
  5  import javafx.scene.Scene;
  6  import javafx.scene.control.Button;
  7  import javafx.scene.layout.BorderPane;
  8  import javafx.scene.layout.HBox;
  9  import javafx.scene.layout.Pane;
 10  import javafx.scene.text.Text;
 11  import javafx.stage.Stage;
 12  
 13  public class LambdaHandlerDemo extends Application {
 14    @Override // Override the start method in the Application class
 15    public void start(Stage primaryStage) {
 16      Text text = new Text(40, 40, "Programming is fun");
 17      Pane pane = new Pane(text);
 18      
 19      // Hold four buttons in an HBox
 20      Button btUp = new Button("Up");
 21      Button btDown = new Button("Down");
 22      Button btLeft = new Button("Left");
 23      Button btRight = new Button("Right");
 24      HBox hBox = new HBox(btUp, btDown, btLeft, btRight);
 25      hBox.setSpacing(10);
 26      hBox.setAlignment(Pos.CENTER);
 27      
 28      BorderPane borderPane = new BorderPane(pane);
 29      borderPane.setBottom(hBox);
 30      
 31      // Create and register the handler
 32      btUp.setOnAction((ActionEvent e) -> {
 33          text.setY(text.getY() > 10 ? text.getY() - 5 : 10);
 34      });
 35  
 36      btDown.setOnAction((e) -> {
 37        text.setY(text.getY() < pane.getHeight() ? 
 38          text.getY() + 5 : pane.getHeight());
 39      });
 40      
 41      btLeft.setOnAction(e -> {
 42        text.setX(text.getX() > 0 ? text.getX() - 5 : 0);
 43      });
 44      
 45      btRight.setOnAction(e ->
 46        text.setX(text.getX() < pane.getWidth() - 100?
 47          text.getX() + 5 : pane.getWidth() - 100)
 48      );
 49  
 50      // Create a scene and place it in the stage
 51      Scene scene = new Scene(borderPane, 400, 350);
 52      primaryStage.setTitle("AnonymousHandlerDemo"); // Set title
 53      primaryStage.setScene(scene); // Place the scene in the stage
 54      primaryStage.show(); // Display the stage
 55    }
 56    
 57    /**
 58     * The main method is only needed for the IDE with limited
 59     * JavaFX support. Not needed for running from the command line.
 60     */
 61    public static void main(String[] args) {
 62      launch(args);
 63    }
 64  }