Question

I'm working in NetBeans 8 with Java 8/JavaFX.

I have an application that starts a main stage based on an fxml file.

There is a menu option for a user to bring up a second stage on request. The function that opens the window looks like this:

@FXML
private void openChildWindow(ActionEvent event) throws Exception {

        Group root = new Group();
        Stage stage = new Stage();

        AnchorPane frame = FXMLLoader.load(getClass().getResource("second.fxml"));
        root.getChildren().add(frame);
        Scene scene = new Scene(root);

        stage.setScene(scene);
        stage.show();


    }

The content of second.fxml looks like this (after all the import statements):

<AnchorPane id="AnchorPane" prefHeight="680.0" prefWidth="1020.0" xmlns:fx="http://javafx.com/fxml/1" >
    <stylesheets><URL value="@css/mycss.css" /></stylesheets>
    <children>
    <TabPane AnchorPane.leftAnchor="0.0" AnchorPane.topAnchor="10.0"
          AnchorPane.bottomAnchor="10.0" AnchorPane.rightAnchor="0.0"  >
        <Tab text="A" closable="false"></Tab>
    </TabPane>
    </children>
</AnchorPane>

The problem: when I click on the corner of the second window to drag and resize it, the contents are not resizing. What am I missing or what do I need so that it will auto-resize?

Was it helpful?

Solution 2

Instead of using a Group, please put your AnchorPane directly into your Scene

@FXML
private void openChildWindow(ActionEvent event) throws Exception {

    Stage stage = new Stage();
    AnchorPane frame = FXMLLoader.load(getClass().getResource("second.fxml"));
    Scene scene = new Scene(frame );
    stage.setScene(scene);
    stage.show();

}

OTHER TIPS

Group is not directly resizable as stated in its javadoc. So it does not being resized by the scene while the window is resized from the corner. You can use the subclasses of Pane instead of. For example try with

VBox root = new VBox();

A Group cannot be resized, "A Group will take on the collective bounds of its children and is not directly resizable." http://docs.oracle.com/javafx/2/api/javafx/scene/Group.html

Replace Group with a different of layout such as StackPane, or BorderPane.

I struggled with the same thing, if we only had read the documentation. :D

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top