0

I have a problem which I'm struggling with for the past few days and deadline for the project is coming. I'mamking a JavaFX desktop app and I encountered an issue: I created GUI with SceneBuilder and my menu is supposed to be dynamic (get items from the database and throw into the menu). I tried to do it and the issue is when I run the app menu doesn't change. What I'm doing wrong?

Main.java

 public void start(Stage primaryStage) throws Exception{

    Parent root = FXMLLoader.load(getClass().getResource("home.fxml"));
    primaryStage.setTitle("Whatever");
    MainController.setMenu();
    primaryStage.setScene(new Scene(root, 1200, 800));

    Rectangle2D primaryScreenBounds = Screen.getPrimary().getVisualBounds();
    primaryStage.setX(primaryScreenBounds.getMinX());
    primaryStage.setY(primaryScreenBounds.getMinY());
    primaryStage.setWidth(primaryScreenBounds.getWidth());
    primaryStage.setHeight(primaryScreenBounds.getHeight());
    primaryStage.show();

}

public static void main(String[] args) throws ClassNotFoundException, SQLException {
    launch(args);

}

MainController.java

 public static void setMenu() throws SQLException, ClassNotFoundException {
     myMenu = new Menu();
    String myQuery = "SELECT name FROM USER.TABLE1";
    Connection connection = DBConnect.connect();
    Statement statement = connection.createStatement();
    ResultSet data = statement.executeQuery(myQuery);

    while (data.next()) {
        System.out.println(data.getString(1));//works
        String dataString = data.getString(1);
       myMenu.getItems().add(new MenuItem(dataString));
    }

    }
Ada
  • 1
  • You load the fxml first and then you call the `setMenu` method. I don't know how you access the menu from the controller, but probably the data isn't there when you retrieve it in the controller e.g. in the `initialize` method. You may find a better approach than using `static` members in the answers to this question anyways: https://stackoverflow.com/questions/14187963/passing-parameters-javafx-fxml – fabian Jan 03 '18 at 12:16
  • Seems like something to try. Thanks – Ada Jan 03 '18 at 13:19
  • If you created the `Menu` using `FXML`, then `myMenu = new Menu();` is invalid code. – SedJ601 Jan 03 '18 at 14:31

1 Answers1

1

You initialized a new Menu object in MainController, and fill this with some MenuItem, but I dont see where did you add this menu to your view.

The menu appear in your view, and it is empty?

Ansaldos
  • 178
  • 7
  • The menu is in the view because it was created with Scene Builder and is in FXML file. The problem is how to edit it aka. add new MenuItems – Ada Jan 03 '18 at 12:22
  • Did you declared your Menu in the controller? (@FXML private Menu myMenu) If yes, you dont have to initialize it again. – Ansaldos Jan 03 '18 at 12:40
  • @Ada But the menu clearly *isn't* created in FXML. You may create *another* menu in the FXML file, but the menu you are populating with menu items is created right there in the code you posted: `myMenu = new Menu()`. – James_D Jan 24 '18 at 00:57