JavaFX Text bind to StringProperty

Description

JavaFX Text bind to StringProperty

import javafx.application.Application;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuBar;
import javafx.scene.control.MenuItem;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyCodeCombination;
import javafx.scene.input.KeyCombination;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.stage.Stage;

public class Main extends Application {

    public static void main(String[] args) {
        Application.launch(args);/*from  w  ww  . j  av  a  2 s  .  com*/
    }

    @Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle("Associating Keyboard Sequences");
        Group root = new Group();
        Scene scene = new Scene(root, 530, 300, Color.WHITE);

        final StringProperty statusProperty = new SimpleStringProperty();


        final Text status = new Text();
        status.setX(100);
        status.setY(50);
        status.setFill(Color.LIME);
        status.setFont(Font.font(null, FontWeight.BOLD, 35));
        status.setTranslateY(50);
        status.textProperty().bind(statusProperty);
        statusProperty.set("Keyboard Shortcuts \nCtrl-N");
        root.getChildren().add(status);

        MenuBar menuBar = new MenuBar();
        menuBar.prefWidthProperty().bind(primaryStage.widthProperty());
        root.getChildren().add(menuBar);

        Menu menu = new Menu("File");
        menuBar.getMenus().add(menu);

        MenuItem newItem = new MenuItem();
        newItem.setText("New");
        newItem.setAccelerator(new KeyCodeCombination(KeyCode.N, KeyCombination.CONTROL_DOWN));
        newItem.setOnAction(new EventHandler<ActionEvent>() {
                        public void handle(ActionEvent event) {
                            statusProperty.set("Ctrl-N");
                        }
                    });
        menu.getItems().add(newItem);


        primaryStage.setScene(scene);
        primaryStage.show();
    }
}



PreviousNext

Related