How do I make a base GUI function which uses any pane

rodude123 :

I have been working with JavaFX and was wondering if I could make a base GUI function which takes any pane as a parameter i.e., not just a VBox.

I can have multiple functions which take in a border pane or HBox or tilepane but I don't want multiple functions just one. I even tried casting the pane to a node but didn't work.

Here is where I use it

private void menu()
{
    VBox root = new VBox();
    Label title = new Label("Quiz");
    title.setStyle("-fx-font-size: 18px; -fx-font-weight: bold;");
    Button btnQuiz = new Button("Quiz");
    btnQuiz.setOnAction(event -> quiz());
    btnQuiz.setMaxWidth(75);
    Button btnCreate = new Button("Create");
    btnCreate.setOnAction(event -> create());
    btnCreate.setMaxWidth(75);
    Button btnQuit = new Button("Quit");
    btnQuit.setOnAction(event -> System.exit(0));
    btnQuit.setMaxWidth(75);
    baseGUI(root, title, btnQuiz, btnCreate, btnQuit);
}

Here is the baseGUI code

private void baseGUI(VBox root, Node... nodes)
{
    for (Node node : nodes)
    {
        root.getChildren().add(node);
    }
    root.setPadding(new Insets(10));
    root.setAlignment(Pos.CENTER);
    root.setSpacing(10.00);
    Scene scene = new Scene(root, 250, 250);
    primaryStage.setScene(scene);
    primaryStage.show();
}

In the end, I should have one function which takes in a root parameter that can handle any type of pane, and another Node of nodes. Thanks for any help.

rodude123 :

I managed to this in the end but thanks for anyone that helped

/**
 * baseGUI function
 * Creates a baseGUI function
 * @param root
 * @param nodes
 */
private void baseGUI(Pane root, Node... nodes)
{
    if (root instanceof VBox)
    {
        ((VBox) root).setAlignment(Pos.CENTER);
        ((VBox) root).setSpacing(10);
        ((VBox)root).getChildren().add(nodes);
    }
    root.setPadding(new Insets(10));
    Scene scene = new Scene(root, 250, 250);
    primaryStage.setScene(scene);
    primaryStage.show();
}

For future reference, this is a base GUI function that takes in nodes as a parameter and a pane and outputs to the screen.

Update:

Changed method so it now works for any pane

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=307440&siteId=1