https://forums.oracle.com/forums/message.jspa?messageID=10219772
If you set the text in the constructor, the center of the text is in the center of the control.
But if you set the text programmatically (and the text was previously empty), then then the left side of the text is in the center of the control.
final TextField tf = new TextField();
Button btn = new Button("update");
btn.setOnAction(new EventHandler<ActionEvent>(){
@Override
public void handle(ActionEvent t) {
tf.setText("Hello");
}
});
tf.setAlignment(Pos.CENTER);
tf.setMinSize(200, 24);
tf.setMaxSize(200, 40);
tf.setPrefSize(200, 40);
tf.setStyle("-fx-font: 20 arial;");
If you set the text in the constructor, the center of the text is in the center of the control.
But if you set the text programmatically (and the text was previously empty), then then the left side of the text is in the center of the control.
final TextField tf = new TextField();
Button btn = new Button("update");
btn.setOnAction(new EventHandler<ActionEvent>(){
@Override
public void handle(ActionEvent t) {
tf.setText("Hello");
}
});
tf.setAlignment(Pos.CENTER);
tf.setMinSize(200, 24);
tf.setMaxSize(200, 40);
tf.setPrefSize(200, 40);
tf.setStyle("-fx-font: 20 arial;");
- duplicates
-
JDK-8127445 TextField text alignment fails when delayed setText() call with alignment = Pos.CENTER
-
- Resolved
-