While working on a "MoneyField" sample I encountered the following bug which appears to make it impossible for me to use right-aligned text field as the implementation. As you change focus, the text is no longer right aligned, and if you change focus and then type and tab out of the text field, the text is scrolled off to the right. The bug for some reason happens due to the fact that I am changing the text when focus changes (so that the $ is removed or added based on whether the field has focus).
public class RightAlignedTextApplication extends Application {
@Override public void start(Stage stage) throws Exception {
final TextField field = new TextField();
field.setAlignment(Pos.BASELINE_RIGHT);
field.setText("$2.50");
final TextField field2 = new TextField();
field2.setAlignment(Pos.BASELINE_RIGHT);
field2.setText("$2.50");
field.focusedProperty().addListener(new InvalidationListener() {
@Override public void invalidated(Observable observable) {
if (field.isFocused()) {
field.setText(field.getText().substring(1));
} else {
field.setText("$" + field.getText());
}
}
});
field2.focusedProperty().addListener(new InvalidationListener() {
@Override public void invalidated(Observable observable) {
if (field2.isFocused()) {
field2.setText(field2.getText().substring(1));
} else {
field2.setText("$" + field2.getText());
}
}
});
VBox root = new VBox();
root.setFillWidth(false);
root.setAlignment(Pos.CENTER);
root.getChildren().addAll(field, field2);
stage.setScene(new Scene(root, 640, 480));
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
public class RightAlignedTextApplication extends Application {
@Override public void start(Stage stage) throws Exception {
final TextField field = new TextField();
field.setAlignment(Pos.BASELINE_RIGHT);
field.setText("$2.50");
final TextField field2 = new TextField();
field2.setAlignment(Pos.BASELINE_RIGHT);
field2.setText("$2.50");
field.focusedProperty().addListener(new InvalidationListener() {
@Override public void invalidated(Observable observable) {
if (field.isFocused()) {
field.setText(field.getText().substring(1));
} else {
field.setText("$" + field.getText());
}
}
});
field2.focusedProperty().addListener(new InvalidationListener() {
@Override public void invalidated(Observable observable) {
if (field2.isFocused()) {
field2.setText(field2.getText().substring(1));
} else {
field2.setText("$" + field2.getText());
}
}
});
VBox root = new VBox();
root.setFillWidth(false);
root.setAlignment(Pos.CENTER);
root.getChildren().addAll(field, field2);
stage.setScene(new Scene(root, 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-8127875 Right aligned text fields don't reposition text correctly when text is programatically updated
-
- Closed
-