Reputation: 39
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();
}
}
Upvotes: 1
Views: 437
Reputation: 39
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);
Upvotes: 2