more examples en
    
 

SHUTTING DOWN A PROGRAM EITHER BY KLICKING ON A BUTTON OR BY TYPING A SHORTCUT

API

javafx.application.Application
javafx.application.Platform
javafx.event.ActionEvent
javafx.event.EventHandler
javafx.geometry.HPos
javafx.geometry.Insets
javafx.geometry.Pos
javafx.scene.Scene
javafx.scene.control.Button
javafx.scene.control.Label
javafx.scene.input.KeyEvent
javafx.scene.layout.GridPane
javafx.scene.text.TextAlignment
javafx.stage.Stage




 

 



This program can be terminated by either clicking on a button or typing the shortcut CTRL+X.

 

 1 package at.krux.javafx.events;

 2 import javafx.application.Application;
 3 import javafx.application.Platform;
 4 import javafx.event.ActionEvent;
 5 import javafx.event.EventHandler;
 6 import javafx.geometry.HPos;
 7 import javafx.geometry.Insets;
 8 import javafx.geometry.Pos;
 9 import javafx.scene.Scene;
10 import javafx.scene.control.Button;
11 import javafx.scene.control.Label;
12 import javafx.scene.input.KeyEvent;
13 import javafx.scene.layout.GridPane;
14 import javafx.scene.text.TextAlignment;
15 import javafx.stage.Stage;

16 public class SceneKeyEvent extends Application {

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

18   public void start(Stage stage) {
19     Label label = new Label("zum beenden auf \"X\" klicken oder STRG+X drücken");
20     Button exit = new Button("X");
21     GridPane pane = new GridPane();

22     label.setPadding(new Insets(20));
23     label.setWrapText(true);
24     label.setTextAlignment(TextAlignment.CENTER);
25     pane.setAlignment(Pos.CENTER);
26     GridPane.setHalignment(exit, HPos.CENTER);

27     pane.add(label, 0, 0);
28     pane.add(exit, 0, 1);

29     Scene scene = new Scene(pane, 300, 300);

30     exit.setOnAction(new EventHandler() {
31       public void handle(ActionEvent actionEvent) {
32         Platform.exit();
33       }
34     });

35     scene.setOnKeyPressed(new EventHandler() {
36       public void handle(KeyEvent keyEvent) {
37         if(keyEvent.isControlDown() && keyEvent.getCode().getChar().equals("X")) {
38           Platform.exit();
39         }
40       }
41     });

42     stage.setScene(scene);
43     stage.show();
44   }
45 }