Home > database >  JavaFX - increase and decrease text size dynamic using CSS and FXML
JavaFX - increase and decrease text size dynamic using CSS and FXML

Time:10-10

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?

CodePudding user response:

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);
  • Related