JavaFX: Setting up menu in a window

This entry is part 40 of 55 in the series JavaFX

In the old days when I first learned about Windows programming using Microsoft Foundation Class, the first thing that I learned was on creating menu items as I thought that was Windows programming.

Now, I realized that there is actually not much to creating menu items.  You create it once and you can use it for a whole lifetime of your application.

In the example below, I have used HBox to position the menu bar to be at the top of the window.  The menu bar will stretch across the window as I have set it with priority.ALWAYS.

package javafxapplication30;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuBar;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.stage.Stage;

public class JavaFXApplication30 extends Application {

    public void start(Stage stage) {

        stage.setTitle("JavaFX Program");

        HBox pane = new HBox();

        Scene scene = new Scene(pane, 400, 350);

        final Menu menu1 = new Menu("File");
        final Menu menu2 = new Menu("Options");
        final Menu menu3 = new Menu("Help");

        MenuBar menuBar = new MenuBar();
        menuBar.getMenus().addAll(menu1, menu2, menu3);

        HBox.setHgrow(menuBar, Priority.ALWAYS);  //So that the menuBar will take the whole HBox


    public static void main(String[] args) {



Series Navigation<< JavaFX: Setting an exact height for list view
JavaFX: Creating Menu Items >>

Leave a comment

Leave a Reply