I don't know if Platform.runLater() is mandatory for setText but it doesn't change anything to use it or not.
package test;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
public class Test extends Application {
public static void main(String[] args) {
launch();
}
@Override
public void start(Stage stage) throws Exception {
BorderPane bp = new BorderPane();
final TextField tf = new TextField();
tf.setAlignment(Pos.CENTER);
tf.setPrefColumnCount(20);
tf.setMaxHeight(20);
tf.setMaxWidth(200);
bp.setCenter(tf);
Scene scene = new Scene(bp, 300, 300);
stage.setScene(scene);
stage.show();
new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
}
// tf.setText("try to center this !"); -- same behavior as with
// Platform.runLater()
Platform.runLater(new Runnable() {
@Override
public void run() {
tf.setText("try to center this !");
}
});
}
}).start();
}
}
- duplicates
-
JDK-8116852 TextField.setText does not update text start position
-
- Closed
-
-
JDK-8127191 Text is not centered in the textfield if the text is set programatically
-
- Closed
-
-
JDK-8127318 Right-aligned text fields position text incorrectly when text changed while receiving focus
-
- Closed
-
-
JDK-8127875 Right aligned text fields don't reposition text correctly when text is programatically updated
-
- Closed
-
- relates to
-
JDK-8127596 Right aligned TextField behaves incorrectly when containing long text
-
- Resolved
-
-
JDK-8127615 PromptText alignment is always left, even with the center-aligned text field.
-
- Closed
-