ZeppRock ZeppRock - 1 month ago 7
Java Question

Drop down menu behaving strange

I am trying to make a drop down menu in javaFX. I get the menu to display but they show as "..." instead of the name of the menu. The other problem is when i click the drop down menu it drops the option inside it and then right after pops it up again. The content of the drop down menu shows correctly.

This is where javaFX inits and shows the scene with it's content (Only the drop down menues).

@Override
public void start(Stage primaryStage) throws Exception {
primaryStage.setTitle("Memory");

BorderPane root = new BorderPane();
Scene scene = new Scene(root , HEIGHT, LENGHT);

canvas = new Canvas(HEIGHT, LENGHT);

menuBar = new GameMenu();

root.getChildren().add(menuBar.createMenu());

primaryStage.setScene(scene);
primaryStage.setResizable(false);
primaryStage.show();
}


This is Where I create the menu bar.

public class GameMenu {

MenuBar menuBar;

public GameMenu(){
menuBar = new MenuBar();

Menu menuFile = new Menu("File");
MenuItem optionStartGame = new MenuItem("New game");
MenuItem optionLoadGame = new MenuItem("Load Game");
menuFile.getItems().addAll(optionStartGame, optionLoadGame);

Menu menuEdit = new Menu("Edit");
Menu menuView = new Menu("View");

menuBar.getMenus().addAll(menuFile, menuEdit, menuView);
}

public MenuBar createMenu() {
return menuBar;
}


}

Answer

The only problem with your code is the you are using getChildren method on your root BorderPane to populate it.

You can add the MenuBar to the top of the BorderPane like:

root.setTop(menuBar.createMenu());

Documentation here on SO about how to use a BorderPane.

Comments