1

Check handle method bellow:

I print out font size and style of TextField, then i set its style to a new style which defines font size as size of its font, then i print out its font size and style again:

public class Control extends Application
{
    @Override
    public void start(Stage primaryStage) throws Exception
    {
        VBox vbox = new VBox();
        final TextField textField = new TextField();
        Button button = new Button("set text and style");
        button.setOnAction(new EventHandler<ActionEvent>()
        {
            public void handle(ActionEvent event)
            {
                System.out.println();
                System.out.println("Font size before: " +textField.getFont().getSize());
                System.out.println("Style before: " +textField.getStyle());
                textField.setText("button clicked");
                textField.setStyle("-fx-font-size: " + textField.getFont().getSize() + ";");
                System.out.println("Font size after: " + textField.getFont().getSize());
                System.out.println("Style after: " + textField.getStyle());
            }
        });
        textField.setStyle("-fx-font-size: 20;");
        vbox.getChildren().add(textField);
        vbox.getChildren().add(button);
        primaryStage.setScene(new Scene(vbox));
        primaryStage.show();
    }
    public static void startApp(String[] args)
    {
        launch(args);
    }
}

I created this class as minimal example.

This is output:

Font size before: 20.0
Style before: -fx-font-size: 20;
Font size after: 11.686956405639648
Style after: -fx-font-size: 20.0;

Font size before: 20.0
Style before: -fx-font-size: 20.0;
Font size after: 20.0
Style after: -fx-font-size: 20.0;

Font size before: 20.0
Style before: -fx-font-size: 20.0;
Font size after: 20.0
Style after: -fx-font-size: 20.0;
...

As you can see, after first time it's style is changed, the Font size is 11.68..., which is weird to me, why this happens?

Using openjfx 15.0.1.

Steps to reproduce:

  1. Copy Control class, which is above and import necessary things
  2. Include Javafx dependencies of course
  3. Create another class with main method where you call Control.startApp(args)
  4. Run and click button few times
Wortig
  • 963
  • 2
  • 11
  • 37
  • No clue why; it's likely something to do with the implementation of the layout. If you add a listener to the text field's `fontProperty` you'll see the font is changed to one with default size when you change the style, and then "immediately" (I'm guessing during the layout pass) changed to one with the specified size. Note the first time the button is pressed is the *only* time the style property changes. If you change the initialization to `textField.setStyle("-fx-font-size: 20.0;");` (which is what the handler sets it to) the issue disappears. – James_D Mar 25 '21 at 13:56
  • hmm ... worksforme (win10, current fx dev, something like fx16++, didn't check others) – kleopatra Mar 25 '21 at 14:29
  • ahh .. no, was wrong: I see the "small" size logged, just properly rounded (that's why I thought all okay) BTW: the behavior is the same with direct/indirect (= from another class) launching, not sure why you included that into your description to reproduce? – kleopatra Mar 25 '21 at 14:36
  • @kleopatra I included it because I am also using Maven to manage dependencies and module-info.java. These two things (probably just Maven) make me unable to make it one class implementation, but separate main method to another class. (I am aware that it can be probably done with some Maven setup, but I never looked at it deeply and I am used to this). – Wortig Mar 25 '21 at 15:41
  • thanks for the clarification :) – kleopatra Mar 25 '21 at 15:42
  • @James_D Please you you explain what do you mean by initialization in: "If you change the initialization to..." I do not get what i can change. – Wortig Mar 25 '21 at 20:26
  • Change `textField.setStyle("-fx-font-size: 20;")` to `textField.setStyle("-fx-font-size: 20.0;")`. – James_D Mar 25 '21 at 20:29
  • My point is that I think the extra font change happens if and only if you change the style. In your example code, the only time the style actually changes is the first time the button is pressed; on subsequent button presses you're setting the style to the value it already has, so no listeners to the style property will be triggered. Does it matter that the font is temporarily set to another value? – James_D Mar 25 '21 at 20:35
  • @James_D Wow, the problem really disappears. So, can we assume that problem was that there was not a double value, but int? – Wortig Mar 26 '21 at 13:49
  • No, the issue arises when the `style` (which is a `String`) changes. I wonder why you care, though. Does it matter if the font changes during a time when it's not being rendered? – James_D Mar 26 '21 at 13:58
  • @James_D So style is changed when its 20.0, but not when it is 20? I have a bigger project where this causes visual error when first time font size is changed this way, but is correct when font size changed more times. – Wortig Mar 26 '21 at 15:17
  • Again, **it's a `String`**. It changes if the sequence of characters is different to the previous sequence of characters. – James_D Mar 26 '21 at 16:13

0 Answers0