Binding string value from TextField to Stage Title : TextField « JavaFX « Java






Binding string value from TextField to Stage Title

 



import javafx.application.Application;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.event.EventHandler;
import javafx.geometry.VPos;
import javafx.scene.Group;
import javafx.scene.GroupBuilder;
import javafx.scene.Scene;
import javafx.scene.SceneBuilder;
import javafx.scene.control.CheckBox;
import javafx.scene.control.CheckBoxBuilder;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.control.TextFieldBuilder;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.HBoxBuilder;
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;
import javafx.stage.WindowEvent;

public class Main extends Application {
  StringProperty title = new SimpleStringProperty();
  
  public static void main(String[] args) {
    Application.launch(args);
  }
  
  @Override
  public void start(Stage stage) {
    final Stage stageRef = stage;
    Group rootGroup;
    TextField titleTextField;
    Scene scene  = SceneBuilder.create()
      .width(270)
      .height(370)
      .root(
        rootGroup = GroupBuilder.create()
          .children(
                HBoxBuilder.create()
                  .spacing(10)
                  .children(
                    new Label("title:"),
                    titleTextField = TextFieldBuilder.create()
                      .text("Stage Coach")
                      .prefColumnCount(15)
                      .build()
              )
              .build()
          )
          .build()
      )
      .build();
    title.bind(titleTextField.textProperty());
    
    stage.setScene(scene);
    stage.titleProperty().bind(title);


    stage.show();
  }
}

   
  








Related examples in the same category

1.Set and clear value for TextField
2.Adding ContextMenu to TextField
3.set Prompt Text For TextField
4.Override replaceText and replaceSelection to create customized TextField