FontMetrics

Sverre Moe sverre.moe at gmail.com
Thu Oct 4 16:56:31 UTC 2018


We are doing something similar to "calculate" the width of an Label in
JavaFX.

        final String name = nameLabel.getText();
        final Text text = new Text(name);
        new Scene(new Group(text));
        text.applyCss();
        double prefWidthVal = text.getLayoutBounds().getWidth();

JavaFX has FontMetrics, but it is private API.
com.sun.javafx.tk.FontMetrics;
com.sun.javafx.tk.Toolkit;

There is an 8 year old feature request for FontMetrics in JavaFX:
https://bugs.openjdk.java.net/browse/JDK-8090775

A public FontMetrics API would be very useful.

/Sverre

Den tor. 4. okt. 2018 kl. 14:42 skrev Andrea Liana <
andrea.liana at ingliana.com>:

> Hello,
>
> Its my first time I contribute to a mailing list, maybe not in proper
> way, but lets talk about Font Metrics.
>
> I've been tinkering fro Java 8 with JavaFX and still I am.
>
> I am working on framework in order to replace my old AWT/Swing based one
> in production.
>
> About FontMetrics, it was one of the first problem I encountered when I
> built from scratch a new table component. Sorry for JavaFX, but the
> default one is quite hard to manage. To solve that, after digging all
> around, the easiest way to manage was to create an instance of
> javafx.scene.text.Text in the skin class and use it when calculating
> layout and text position.
>
> The code looks like:
>
> private final Texttext =new Text(); ....
> text.setText(column._title); text.setFont(HeaderFont); final Bounds
> bounds = text.getLayoutBounds(); ... // Use bounds for text measurement...
>
>
> Maybe its not the best or elegant way, but for the moment it works. In
> my opinion font measurements should be baked into the Font class
> directly instead of adding more classes.
>
> Andrea Liana
>
>
>
>
>


More information about the openjfx-discuss mailing list