JavaFX 8 Tutorial 54 - Centering Text In Scene




package tutorial.pkg54.centering.text;

import javafx.application.Application;
import javafx.geometry.VPos;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.stage.Stage;

/**
 *
 * @author Ram
 */
public class Tutorial54CenteringText extends Application {
   
    @Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle("JavaFX 8 Tutorial 54 - Centering Text In Scene");
        Group root = new Group();
        Scene scene = new Scene(root, 400, 200);
       
        Text text = new Text("JavaFX 8 Tutorial 54 - Centering Text In Scene");
        text.setTextOrigin(VPos.TOP);
        text.setFont(Font.font(null, FontWeight.BOLD, 15));
       
        text.layoutXProperty().bind(scene.widthProperty().subtract(text.prefWidth(-1)).divide(2));
        text.layoutYProperty().bind(scene.heightProperty().subtract(text.prefHeight(-1)).divide(2));
       
        root.getChildren().add(text);
             
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }
   
}

Comments

Popular posts from this blog

JavaFX 8 Tutorial 62 - Jasper Report

JavaFX 8 Tutorial 64 Spring Boot CRUD with FXML