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:
- Copy Control class, which is above and import necessary things
- Include Javafx dependencies of course
- Create another class with main method where you call Control.startApp(args)
- Run and click button few times