TextField right alignment issue javafx


Question

I have textfield that containing a value and that value should be Right alignment.

When i run the application it display the text left alignment but i set the Right alignment. and problem with 3rd textfield.

After clicking the update button it works fine.

So may i know why it is behave different.

Code:

    import java.io.File;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.VBox;
import javafx.stage.FileChooser;
import javafx.stage.Stage;

public class TextFieldAlignment extends Application {

    TextField rText;

    File file;

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage stage) {
        rText = new TextField("updated right1 updated right2 updated right3 updated right4");
        rText.setAlignment(Pos.CENTER_RIGHT);

        Button btn = new Button("update");
        btn.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent t) {
                rText.setText("updated right1 updated right2 updated right3 updated right4");

//              applyWorkaround();
            }
        });     

        final Label labelFile = new Label();

          Button btn2 = new Button();
          btn2.setText("Open FileChooser'");
          btn2.setOnAction(new EventHandler<ActionEvent>() {

              @Override
              public void handle(ActionEvent event) {
                  FileChooser fileChooser = new FileChooser();

                  //Set extension filter
                  FileChooser.ExtensionFilter extFilter = new FileChooser.ExtensionFilter("AVI files (*.exml)", "*.exml");
                  fileChooser.getExtensionFilters().add(extFilter);

                  //Show open file dialog
                  file = fileChooser.showOpenDialog(null);

//                rText.setText(file.getPath());
              }
          });

        VBox root = new VBox();
        root.getChildren().addAll(rText, btn, btn2);

        stage.setScene(new Scene(root, 200, 300));
        stage.show();
    }

}
1
0
10/1/2014 9:03:06 AM

Put the setalignment and settext after you add the TextField to the Scene.

Ref: Java API docs for Node

Node objects may be constructed and modified on any thread as long they are not yet attached to a Scene. An application must attach nodes to a Scene, and modify nodes that are already attached to a Scene, on the JavaFX Application Thread.

0
10/1/2014 9:23:27 AM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon