0

I would like to be able to dynamically change font size in a TextArea by percentage. Between 25% - 225%. Eventually this setting will be loaded from a configuration file.

Current code in FXML: <TextArea fx:id="txtDescription" layoutY="25.0" prefHeight="471.0" prefWidth="1036.0" />

Current code in CSS: .text-area { -fx-font-size: 25; }

In MainController.class I have imported the reference @FXML private TextArea txtDescription;

But if I try to retrieve information as below in a simple way, I get none...

System.out.println("Get Style: " + txtDescription.getStyle()); System.out.println("Get StyleNode: " + txtDescription.getStyleableNode());

What steps do you need to take to dynamically change the font size?

Mizzy
  • 23
  • 5

1 Answers1

0

Java

double initialSize = 16.0; // 100% size
        
Slider slider = new Slider(25, 225, 100);
slider.valueProperty().addListener((observable,oldValue,newValue) -> { 
     double fromPercent = (initialSize * newValue) / 100           
     txtDescription.setStyle("-fx-font-size:" + (int) fromPercent);
});

For get font size

txtDescription.getFont().getSize();

For change without rewrite style

    Font oldFont = txtDescription.getFont();
    Font newFontSize = new Font(txtDescription.getName(), newSize);
    txtDescription.setFont(newFontSize);
Sergey
  • 16
  • 2
  • Appreciate your prompt response (y) – Mizzy Oct 09 '22 at 19:48
  • An example if this approach is in [javafx automatic resizing and button padding](https://stackoverflow.com/questions/23229149/javafx-automatic-resizing-and-button-padding). – jewelsea Oct 10 '22 at 13:26