1

What I'm trying to do:
Make a base navigation HBox nav (also called HBox nav in the code), then use that HBox nav to make specialised navigation systems for each scene/page (sort of like a main class and then sub classes, without using classes).

Issue:
The HBox simply isn't showing. It was working initially, when I individually made a unique navigation HBox for each page, but when I tried to make 1 main navigation system, which gets customized for every page, it stopped showing.

//imports:
package com.example.phonecustomiser;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import java.io.IOException;
//actual code:
public class HelloApplication extends Application {
    @Override
    public void start(Stage stage) throws IOException {

        //universal buttons
        Button playButton = new Button("PLAY");
        playButton.setPrefWidth(100);
        Button menuButton = new Button("MENU PAGE");
        menuButton.setPrefWidth(100);
        Button startButton = new Button( "START PAGE");
        menuButton.setPrefWidth(100);


        //universal navigation
        HBox nav = new HBox();                        // <------ HBOX NAV
        nav.setMaxHeight(100);
        nav.setStyle("-fx-background-color: white");
        nav.setPadding(new Insets(40, 40, 40, 40));
        nav.setSpacing(10);
        nav.getChildren().addAll(playButton, menuButton);


        //startPage
        StackPane startCanvas = new StackPane();
        HBox startNav = nav;                               // <--- SPECIALISED NAV
        startNav.setStyle("-fx-background-color: red");
        startCanvas.getChildren().addAll(startNav);        // <--- NOT SHOWING

        //menuPage
        StackPane menuCanvas = new StackPane();
        HBox menuNav = nav;
        menuNav.setStyle("-fx-background-color: green");
        menuCanvas.setAlignment(menuNav, Pos.BOTTOM_CENTER);
        menuCanvas.getChildren().addAll(menuNav);




        //scenes
        Scene startScene = new Scene(startCanvas, 600, 600);
        Scene menuScene = new Scene(menuCanvas, 600, 600);

        //button functions
        menuButton.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent actionEvent) {
                stage.setScene(menuScene);
            }
        });



        stage.setScene(startScene);
        stage.show();
    }

    public static void main(String[] args) {
        launch();
    }
}
luck9101
  • 43
  • 5
  • 2
    startNav and menuNav are not two different HBox objects. They’re the same HBox. You cannot add a node to two different StackPane parents. You probably should create two HBox objects, each with their own new Buttons. – VGR Jan 13 '22 at 00:35
  • You are doing things that do not make sense to me. For example, you have `HBox nav = new HBox();`. Then you go on further to do the following: `HBox startNav = nav;` and `HBox menuNav = nav`. – SedJ601 Jan 13 '22 at 00:35
  • 1
    You can't do this. You have `HBox nav = new HBox()`. Then you have `HBox startNav = nav`. This means startNav is the same as nav, any properties you set on it will be set on nav and vice-versa. Additionally, you can only a node can only be at a single position in the scene graph at the time, if you try to add it twice, the original node will be removed automatically. Since nav and startNav are the same, it will only appear in the scene graph once. (likely this comment is redundant and a bit slow at posting, but you get the idea...). – jewelsea Jan 13 '22 at 00:37
  • Yes, I understand thanks. I was thinking about them like they are primitive types. I have made a whole new class which holds the HBox so I don't have to remake the whole thing a million times. – luck9101 Jan 13 '22 at 00:39
  • You note: "sort of like a main class and then sub classes, without using classes", perhaps use classes and sub classes or at least multiple instances of a class by calling new multiple times, perhaps from a factory method if that is what you want. – jewelsea Jan 13 '22 at 00:39
  • I've done it this way: https://justpaste.it/78vxu It fills my original intention. – luck9101 Jan 13 '22 at 00:42
  • You can [answer your own question](https://stackoverflow.com/help/self-answer). In this case, I suggest you do so, it is better than commenting with a link to an offsite resource. When you do so, put the code for the answer in the answer, formatted as code. – jewelsea Jan 13 '22 at 00:46

1 Answers1

2

I was thinking about these objects like they are primitive types. So: int monkey = 5; int cow = monkey;

cow++; this does not affect monkey

But objects do not work this way. A work around solution is (that I found at least), making a whole new class and putting my navigation system in there, then calling new instances of the class for every scene/page. The class I made:

package com.example.phonecustomiser;
import javafx.geometry.Insets;
import javafx.scene.control.Button;
import javafx.scene.layout.HBox;

public class Menu {
    Button playButton = new Button("PLAY");
    Button menuButton = new Button("MENU PAGE");
    Button startButton = new Button( "START PAGE");

    HBox nav = new HBox();

    public void setUp(){
        playButton.setPrefWidth(100);
        menuButton.setPrefWidth(100);
        menuButton.setPrefWidth(100);
        nav.setMaxHeight(100);
        nav.setStyle("-fx-background-color: green");
        nav.setPadding(new Insets(40, 40, 40, 40));
        nav.setSpacing(10);
        nav.getChildren().addAll(playButton, menuButton);
    }

}

Implementation of that class:

StackPane startCanvas = new StackPane();
        Menu startNav = new Menu();
        startNav.setUp();
        startNav.nav.setStyle("-fx-background-color: red");
        startCanvas.getChildren().addAll(startNav.nav);
        startCanvas.setAlignment(startNav.nav, Pos.BOTTOM_CENTER);

luck9101
  • 43
  • 5