I am making a cross number puzzle game (cross word with numbers). For the actual puzzle itself, I have a grid pane containing stack panes (the squares of puzzle grid). Some of the stack panes have labels in their corners to denote the start of a question and some of them have text fields for the user to write their answers. I have added a listener to each stack pane so that when its height changes, it will also change the font size of the text field attached to that stack pane. Here is the code:
ChangeListener<Number> squareSizeListener = (observable, oldValue, newValue) -> {
textField.setStyle("-fx-font-size: " + (newValue.intValue() / 4));
};
square.heightProperty().addListener(squareSizeListener);
I use an identical listener to change the size of the label denoting the question number. While this does the job of changing the font size of the text field and question numbers as I resize the window (and hence the stack panes), I have come across a strange problem where, while resizing the window, a smaller version of the text will very briefly appear instead of the text and then quickly dissapear. This smaller text is always the same size and colour. It's as if it reseting the style of the text to a default before applying the new one.
Here is a video of what I am talking about.
I have no clue why this is happening and I have been unable to find anything regarding this online. I've tried changing the stack pane to a different container but encountered the same issue. Could someone please help me figure out whats going on?
Many thanks!
PS. If someone also knows that the outline of the text field is also dissappearing and reappearing, I would be very grateful!
EDIT: So somehow I stumbled upon a solution. I don't know why or how it works, but it does. Calling the applyCss method on the textfield/label after changing their fonts using setStyle seems to fix the problem.