Sunflame
Sunflame

Reputation: 3186

JavaFx: Label text width

I have a problem with the Label's width. If I change the size of the window and the label's width doesn't fit to the window.

I expect something like:

Initial: 012345678901234567890123456789

After resize: 01234567890123...

But the actual state:

Initial state

After resize:

After resize

How can I get my expected result?

Here is the .fxml file

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.HBox?>
<AnchorPane xmlns="http://javafx.com/javafx"
            xmlns:fx="http://javafx.com/fxml"
            fx:controller="stackoverflow.labeltest.Controller">
    <HBox>
        <Label fx:id="label"/>
    </HBox>
</AnchorPane>

Upvotes: 0

Views: 937

Answers (1)

fabian
fabian

Reputation: 82461

An AnchorPane does not resize a child without constraints. You need to set the rightAnchor and leftAnchor constraints of the HBox. (You could also simply use the HBox as root.)

<AnchorPane xmlns="http://javafx.com/javafx"
            xmlns:fx="http://javafx.com/fxml"
            fx:controller="stackoverflow.labeltest.Controller">
    <children>
        <HBox AnchorPane.rightAnchor="0" AnchorPane.leftAnchor="0">
            <children>
                <Label fx:id="label"/>
            </children>
        </HBox>
    </children>
</AnchorPane>

Upvotes: 1

Related Questions