Resizing of Label in AnchorPane

时间:2018-07-25 08:17:01

标签: java javafx javafx-8

I have a JavaFX Label inside an AnchorPane, which is contained in a VBox. This one is in a ScrollPane. I want the text to wrap and to automatically adapt to the width of the window.

Here is a simplified example of my code:

public class MainApp extends Application {

    public static void main(String[] args) {
        Application.launch(MainApp.class);
    }

    @Override
    public void start(Stage primaryStage) {
        final ScrollPane root = new ScrollPane();
        root.setFitToWidth(true);

        VBox vbx = new VBox();
        vbx.setStyle("-fx-background-color: green");
        vbx.setSpacing(20);
        root.setContent(vbx);

        Label lbl = new Label(
                "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, " +
                "sed diam nonumy eirmod tempor invidunt ut labore et dolore " + 
                "magna aliquyam erat, sed diam voluptua. At vero eos et " +
                "accusam et justo duo dolores et ea rebum.");
        lbl.setStyle("-fx-background-color: yellow");
        lbl.setWrapText(true);

        AnchorPane ap = new AnchorPane();
        ap.setStyle("-fx-background-color: blue");
        ap.getChildren().add(lbl);
        vbx.getChildren().add(ap);

        lbl.maxWidthProperty().bind(ap.widthProperty());
        lbl.setTextAlignment(TextAlignment.JUSTIFY);

        AnchorPane.setLeftAnchor(lbl, 0.0);
        AnchorPane.setTopAnchor(lbl, 0.0);
        AnchorPane.setRightAnchor(lbl, 0.0);
        AnchorPane.setBottomAnchor(lbl, 0.0);

        Scene scene = new Scene(root, 300, 250);

        primaryStage.setScene(scene);
        primaryStage.show();
    }
}

The Problem is: When I start the program, the Label is much higher than the available height and the text is centered vertically inside. When I resize the width of the window, the text jumps to the top and its height is reduced to the correct size. But the VBox remains very large.

What do I have to change, that the text is initially in the correct position (at the top) without having to resize the window and how to make the VBox not larger than necessary?

Please note: The AnchorPane is there for some reasons that would take to long to explain, so I cannot remove it. Also I cannot change the Label to Text because it must remain at least anything derived from Region

1 个答案:

答案 0 :(得分:1)

您可能应该VBox添加为ScrollPane中的根节点。我说这是因为SceneBuilder允许我这样做。在此示例中,我将根节点设置为AnchorPane。然后,我添加VBox及其子元素。我将Label's的宽度绑定到Stage

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.ScrollPane;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.VBox;
import javafx.scene.text.TextAlignment;
import javafx.stage.Stage;

public class MainApp extends Application
{

    public static void main(String[] args)
    {
        Application.launch(MainApp.class);
    }

    @Override
    public void start(Stage primaryStage)
    {
        final ScrollPane root = new ScrollPane();
        root.setFitToWidth(true);

        Label lbl = new Label(
                "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, "
                + "sed diam nonumy eirmod tempor invidunt ut labore et dolore "
                + "magna aliquyam erat, sed diam voluptua. At vero eos et "
                + "accusam et justo duo dolores et ea rebum.");
        lbl.setStyle("-fx-background-color: yellow");
        lbl.setWrapText(true);

        AnchorPane ap = new AnchorPane();
        ap.setStyle("-fx-background-color: blue");
        ap.getChildren().add(lbl);

        lbl.setTextAlignment(TextAlignment.JUSTIFY);
        lbl.setMaxHeight(Double.MAX_VALUE);

        VBox vBox = new VBox(ap);
        AnchorPane rootAP = new AnchorPane(vBox);
        root.setContent(rootAP);
        Scene scene = new Scene(root, 300, 250);

        lbl.prefWidthProperty().bind(scene.widthProperty());//This should bind the Label width to the Scene width

        primaryStage.setScene(scene);
        primaryStage.show();
    }
}
相关问题