JavaFX - reusable FXML component

I create a GUI using Scene Builder, and most of my scenes have one thing in common (an iOS type home button at the bottom). I was wondering if this component can be defined in a separate fxml file. From my research, there is a similar process for declaring a reusable component, but only in the same fxml file. How can I apply this principle to multiple fxml files?

+4
source share
2 answers

You can do the following:

<AnchorPane xmlns:fx="http://javafx.com/fxml/1" fx:controller="de.example.MainController">
<children>
<fx:include fx:id="someId" source="NestedFXML.fxml"/>
</children>

<AnchorPane xmlns:fx="http://javafx.com/fxml/1" fx:controller="de.example.NestedFXMLController">
</AnchorPane>

Controller Classes:

public class MainController implements Initializable {

    @FXML
    private NestedFXMLController someIdController;

    @Override
    public void initialize(URL location, ResourceBundle resources) {
    // TODO Auto-generated method stub

    }
}
public class NestedFXMLController implements Initializable {

    @Override
    public void initialize(URL location, ResourceBundle resources) {
    // TODO Auto-generated method stub

    }
}

: FXML. fx: id + ""!

+3

FXML Node , Node , , ? , , FXML.

?

Parent root = FXMLLoader.load(getClass().getResource("fileName.fxml"));

        ObservableList<Node> nodes = root.getChildrenUnmodifiable();
        String _id = "testButton";
        for (Node node : nodes) {
            if (node.getId().equals(_id)) {
                return node;
            }

        }
        return null;
}
0

Source: https://habr.com/ru/post/1653495/


All Articles