0

I am trying to resize the font of my labels whenever i resize my window

my code is

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.scene.text.Font;
import javafx.stage.Stage;

public class Questions extends Application {



    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {

        GridPane info = new GridPane();
        BorderPane display = new BorderPane();

        Font font = new Font("monospace",20);
        Label lblName = new Label("Name");
        Label lblGender = new Label("Gender");
        lblName.setFont(font);
        lblGender.setFont(font);
        info.add(lblName, 0, 0);
        info.add(lblGender, 0, 1);



        display.setCenter(info);
        Scene scene = new Scene(display);
        primaryStage.setTitle("Random NPC Generator");
        primaryStage.setScene(scene);
        primaryStage.setHeight(655);
        primaryStage.setWidth(555);
        primaryStage.show();

    }

}

i have googled forever it seems like and cannot find the answer, so again, Any idea how to resize the labels whenever i resize the main window? Thanks in advance! Edit: I am looking for a way to do it in JavaFX not awf

  • 1
    Possible duplicate of [How to auto-adjust font size of multiple JLabel based on container size in a smooth way?](https://stackoverflow.com/questions/9814616/how-to-auto-adjust-font-size-of-multiple-jlabel-based-on-container-size-in-a-smo) – agillgilla Aug 02 '18 at 21:24
  • Was looking for a way using JavaFX not swing or awt – Samuel Dague Aug 02 '18 at 22:17

1 Answers1

0

Here is an example of how the Font's size of a Label can be resized with the resizing of the window. A ChangeListener is applied to the height and width properties of the Scene - and as the window size is increased or decreased the size of the font is changed accordingly.

The example code:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.scene.control.Label;
import javafx.scene.text.Font;
import javafx.scene.layout.HBox;
import javafx.geometry.Pos;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
public class FxFontChanger extends Application {
    private Label label;
    private static final double MAX_FONT_SIZE = 70.0;
    private static final double MIN_FONT_SIZE = 10.0;
    public static void main(String [] args) {
        launch(args);
    }
    @Override
    public void start(Stage primaryStage) throws Exception {
        Font font = new Font("segoe script", 30);
        label = new Label("Rhyme and Rhythm");
        label.setFont(font);
        HBox hbox = new HBox();
        hbox.setAlignment(Pos.CENTER);
        hbox.getChildren().add(label);
        Scene scene = new Scene(hbox, 600, 400);
        scene.heightProperty().addListener(new SizeChangeListener());
        scene.widthProperty().addListener(new SizeChangeListener());
        primaryStage.setTitle("Font Changer Demo");
        primaryStage.setScene(scene);
        primaryStage.show();
    }
    private class SizeChangeListener implements ChangeListener<Number> {
        @Override
        public void changed(ObservableValue<? extends Number> obVal,
                Number oldVal, Number newVal) {
            // Get change in scene size
            double newSize = newVal.doubleValue();
            double oldSize= oldVal.doubleValue();
            double sizeChange = newSize - oldSize;
            double percentChange = (sizeChange * 100) / oldSize;
            // Apply the change to the label's font size    
            Font f = label.getFont();
            double fontSize = f.getSize();
            double fontChange = fontSize + (fontSize*percentChange);
            fontChange = (fontChange > MAX_FONT_SIZE) ? MAX_FONT_SIZE : fontChange;
            fontChange = (fontChange < MIN_FONT_SIZE) ? MIN_FONT_SIZE : fontChange; 
            label.setFont(new Font(f.getName(), fontChange));
        }
    }
}
prasad_
  • 12,755
  • 2
  • 24
  • 36