Chapter 8 |
Example program
In the previous examples, the mouse and the action events were demonstrated. In fact, there are many more options. In the following example, quite a few of events are demonstrated. Because the number of event classes is quite small, the "change listener" (an instance of the class that implements the ChangeListener
interface) is used for the many graphical components. To implement the ChangeListener
interface, the method changed
must be specified. Various properties of different graphical components can be listened to - for example, changes of the window size (e.g. widthProperty
and heightProperty
of class Stage
). In the following example, the selected list view property selectedItemProperty
is listened to.
public void start(Stage primaryStage) { // create a border pane BorderPane borderpane = new BorderPane(); // create a text field and add it to the top of the border pane TextField textfield = new TextField(); textfield.setText("some text"); borderpane.setTop(textfield); // create and register a handler for the key event // (reacts only on the ENTER key) textfield.setOnKeyPressed(new EventHandler<KeyEvent>() { public void handle(KeyEvent keyEvent) { if (keyEvent.getCode() == KeyCode.ENTER) { textfield.setText("I have pressed the ENTER key"); } } }); // create a list view and add it to the center of the border pane ListView<String> list = new ListView<String>(); ObservableList<String> items = FXCollections.observableArrayList("First", "Second", "Third", "Fourth"); list.setItems(items); borderpane.setCenter(list); // create and register listener for the property of the list view // (property changes if something is selected) list.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<String>() { public void changed(ObservableValue<? extends String> ov, String oldValue, String newValue) { textfield.setText(newValue); } }); // the second border pane is added to the bottom of the first border pane // two buttons are added to the left and right of the second border pane BorderPane borderpane2 = new BorderPane(); Button button1 = new Button("1"); borderpane2.setLeft(button1); Button button2 = new Button("2"); borderpane2.setRight(button2); borderpane.setBottom(borderpane2); // create and register a handler for the mouse event on the second border pane borderpane2.setOnMouseClicked(new EventHandler<MouseEvent>() { public void handle(MouseEvent me) { textfield.setText("new text now"); } }); // create and register a handler for the key event on the first button button1.setOnKeyPressed(new EventHandler<KeyEvent>() { public void handle(KeyEvent keyEvent) { if (keyEvent.getCode() == KeyCode.DIGIT1) { textfield.setText("now 1"); } } }); // create and register a handler for the mouse event on the second button button2.setOnMouseClicked(new EventHandler<MouseEvent>() { public void handle(MouseEvent me) { textfield.setText("now 2"); } }); // create and register a handler for window event primaryStage.setOnHiding(new EventHandler<WindowEvent>() { public void handle(WindowEvent event) { // create the second stage Stage question = new Stage(); // label with a question and two buttons Label label = new Label("Are you sure you want to close the window?"); Button okButton = new Button("Yes"); Button cancelButton = new Button("No"); // create and register a handler for the Yes button okButton.setOnAction(new EventHandler<ActionEvent>() { public void handle(ActionEvent event) { question.hide(); } }); // create and register a handler for the No button cancelButton.setOnAction(new EventHandler<ActionEvent>() { public void handle(ActionEvent event) { primaryStage.show(); question.hide(); } }); // group the buttons FlowPane pane = new FlowPane(10, 10); pane.setAlignment(Pos.CENTER); pane.getChildren().addAll(okButton, cancelButton); // the question and the group of buttons are added to vertical box VBox vBox = new VBox(10); vBox.setAlignment(Pos.CENTER); vBox.getChildren().addAll(label, pane); // create a scene and display the stage Scene scene2 = new Scene(vBox); question.setScene(scene2); question.show(); } }); //the end of the window event // create a scene and display the stage Scene scene = new Scene(borderpane, 300, 150, Color.SNOW); primaryStage.setTitle("Events"); primaryStage.setResizable(false); primaryStage.setScene(scene); primaryStage.show(); }
Try out
Add the start
-method given above to the application and run it. Try to understand how the application works. In particular, you need to look at how the ActionEvent
, KeyEvent
, MouseEvent
and WindowEvent
classes and the interface ChangeListener
for the user interface elements have been used. (See also http://docs.oracle.com/javase/8/javafx/user-interface-tutorial/ui_controls.htm.)
For advanced ones Use lambda expressions where appropriate.
Chapter 8 |