Here is a simple reproducible test case. Run the app and adjust the slider. You will see that the text in the text field is not repositioned such that it remains properly right aligned. This appears to be due to the TextFieldSkin not even being notified of the change in the text. I suspect if, after changing the text, the caret position were repositioned to the end of the text, alignment would work correctly (since TextFieldSkin does pay attention to the caret position). What I am not certain of is what the correct behavior for the caretPosition should be. In a right aligned text field, should the caret always be reset to the very last position when the text is changed manually??
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Slider;
import javafx.scene.control.TextField;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
/**
* The TextFieldSkin has a bug for right or I would imagine center aligned text.
* If you alter the text on the text field in code, the text in the text field
* is updated but not repositioned. I'm not sure why this works as is for left
* aligned text, but perhaps it is due to the caret position not changing
* in the left aligned case whereas in the right aligned case, the caret
* position is adjusted.
*/
public class RightAlignedTextNotPositionedCorrectlyApp extends Application {
@Override public void start(Stage stage) throws Exception {
HBox hbox = new HBox(7);
Slider numberSlider = new Slider();
numberSlider.setMin(0);
numberSlider.setMax(1000000);
TextField field = new TextField();
field.setAlignment(Pos.BASELINE_RIGHT);
field.textProperty().bind(numberSlider.valueProperty().asString());
hbox.getChildren().addAll(numberSlider, field);
hbox.setPadding(new Insets(12));
hbox.setAlignment(Pos.CENTER);
hbox.setFillHeight(false);
stage.setScene(new Scene(hbox, 640, 480));
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Slider;
import javafx.scene.control.TextField;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
/**
* The TextFieldSkin has a bug for right or I would imagine center aligned text.
* If you alter the text on the text field in code, the text in the text field
* is updated but not repositioned. I'm not sure why this works as is for left
* aligned text, but perhaps it is due to the caret position not changing
* in the left aligned case whereas in the right aligned case, the caret
* position is adjusted.
*/
public class RightAlignedTextNotPositionedCorrectlyApp extends Application {
@Override public void start(Stage stage) throws Exception {
HBox hbox = new HBox(7);
Slider numberSlider = new Slider();
numberSlider.setMin(0);
numberSlider.setMax(1000000);
TextField field = new TextField();
field.setAlignment(Pos.BASELINE_RIGHT);
field.textProperty().bind(numberSlider.valueProperty().asString());
hbox.getChildren().addAll(numberSlider, field);
hbox.setPadding(new Insets(12));
hbox.setAlignment(Pos.CENTER);
hbox.setFillHeight(false);
stage.setScene(new Scene(hbox, 640, 480));
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
- duplicates
-
JDK-8127445 TextField text alignment fails when delayed setText() call with alignment = Pos.CENTER
-
- Resolved
-
- relates to
-
JDK-8127318 Right-aligned text fields position text incorrectly when text changed while receiving focus
-
- Closed
-