Binding Stage x, y, width, height to Text control : Text « JavaFX « Java






Binding Stage x, y, width, height to Text control

 

import javafx.application.Application;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.geometry.VPos;
import javafx.scene.Group;
import javafx.scene.GroupBuilder;
import javafx.scene.Scene;
import javafx.scene.SceneBuilder;
import javafx.scene.control.TextField;
import javafx.scene.layout.VBoxBuilder;
import javafx.scene.paint.Color;
import javafx.scene.shape.RectangleBuilder;
import javafx.scene.text.Text;
import javafx.scene.text.TextBuilder;
import javafx.stage.Stage;

public class Main extends Application {
  StringProperty title = new SimpleStringProperty();
  
  Text textStageX;
  Text textStageY;
  Text textStageW;
  Text textStageH;
  Text textStageF;
  public static void main(String[] args) {
    Application.launch(args);
  }
  
  @Override
  public void start(Stage stage) {
    final Stage stageRef = stage;
    Group rootGroup;
    Scene scene  = SceneBuilder.create()
      .width(270)
      .height(370)
      .root(
        rootGroup = GroupBuilder.create()
          .children(
            VBoxBuilder.create()
              .layoutX(30)
              .layoutY(20)
              .spacing(10)
              .children(
                textStageX = TextBuilder.create()
                  .textOrigin(VPos.TOP)
                  .build(),
                textStageY = TextBuilder.create()
                  .textOrigin(VPos.TOP)
                  .build(),
                textStageW = TextBuilder.create()
                  .textOrigin(VPos.TOP)
                  .build(),
                textStageH = TextBuilder.create()
                  .textOrigin(VPos.TOP)
                  .build(),
                textStageF = TextBuilder.create()
                  .textOrigin(VPos.TOP)
                  .build()
              )
              .build()
          )
          .build()
      )
      .build();

    
    textStageX.textProperty().bind(new SimpleStringProperty("x: ")
            .concat(stageRef.xProperty().asString()));
    textStageY.textProperty().bind(new SimpleStringProperty("y: ")
            .concat(stageRef.yProperty().asString()));
    textStageW.textProperty().bind(new SimpleStringProperty("width: ")
            .concat(stageRef.widthProperty().asString()));
    textStageH.textProperty().bind(new SimpleStringProperty("height: ")
            .concat(stageRef.heightProperty().asString()));
    textStageF.textProperty().bind(new SimpleStringProperty("focused: ")
            .concat(stageRef.focusedProperty().asString()));
    stage.setResizable(true);

    
    stage.setScene(scene);
    stage.titleProperty().bind(title);


    stage.show();
  }
}

   
  








Related examples in the same category

1.Create Static Text control and set color
2.Rotate a Text
3.Text with DropShadow
4.Text with CHOCOLATE color and Font.SERIF
5.Line breaking for Text
6.Reflection effect on text
7.Set fill and font for Text
8.Text wrapping width
9.Set Text Font
10.Text class defines a node that displays a text
11.Reflected Text
12.Text Effect show off
13.Paragraphs are separated by '\n' and the text is wrapped on paragraph boundaries.