5

I'm using a Text node to compute the dimensions of a string, as described here, summarized as:

new Text("some text").getLayoutBounds();

The dimensions returned are not always the same at different execution points, for the same input string, and everything else being the same (font, etc.). It seems that the inconsistencies are somehow related to the interaction of the backspace with a TextArea, even if this TextArea is not related to the Text node.

It's an intermittent/non-deterministic problem, but others were able to reproduce it with the following code:

public class Test extends Application {
    private Text text = new Text();
    
    public static void main(String... args) {
        launch(args);
    }
    
    public void start(Stage stage) {
        TextArea textArea = new TextArea();
        textArea.textProperty().addListener((observable, oldValue, newValue) -> {
            text.setText(newValue);
            System.out.println(String.format("height=%.1f", text.getLayoutBounds().getHeight()));
        });

        stage.setScene(new Scene(new Group(textArea)));
        stage.show();
    }
}

Typing a few "X" characters, I get (on Windows): height=16.0. After typing backspace (but when there are still a few "X" characters in the string), I get height=17.0. If I type some more characters, the height stays 17.0, then arbitrarily shifts back to 16.0, then 17.0, etc. Other users report different kinds of erratic behavior.

  • If you replace the TextArea with TextField, the problem goes away (!)
  • It doesn't matter where the Text node is initialized.
  • While debugging, I see that that the valid flag is false in all cases in LazyBoundsProperty#get, so bounds seem to be recomputed for each new character.
  • I get the same behavior on JavaFX 11.0.2 as JavaFX 16.

What causes this inconsistency, and how can I avoid it?

0009laH
  • 1,960
  • 13
  • 27
SDJ
  • 4,083
  • 1
  • 17
  • 35
  • I'm guessing it creates space for the padding between different lines of text at some point (even if it's not strictly needed). I guess the question is why you care about the size of the text displayed by the text area; maybe there is a different way entirely to get at whatever it is you're trying to do. – James_D Jun 03 '21 at 17:00
  • 1
    Thing to note: the problem is not necessarily related to the . When programmatically changing the text to a new String with shorter length (as well for the `Text` as the `TextArea`) the height does also change to 17.0. See also [this gist](https://gist.github.com/aron-hoogeveen/0425fc80fa80cc797bbb310c203d8683) – Aron Hoogeveen Jun 10 '21 at 08:39
  • 1
    Above mentioned Gist makes me suspect that the JavaFX engine reuses some graphical text components for rendering the same (or equal) Strings. Indeed, only when setting the text value of a `TextArea` to that of the `Text` element does the height change occur. (Note however that I have no knowledge whatsoever about the inner workings of JavaFX) – Aron Hoogeveen Jun 10 '21 at 08:46

0 Answers0