import javafx.application.Application; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.control.Label; import javafx.scene.control.Menu; import javafx.scene.control.MenuBar; import javafx.scene.control.MenuItem; import javafx.scene.control.TextField; import javafx.scene.layout.HBox; import javafx.scene.layout.Pane; import javafx.scene.layout.VBox; import javafx.stage.Stage; public class MenuBarSample extends Application { public final static String TEST_PANE_ID = "TestPane"; public final static String CLEAR_BTN_ID = "Clear"; public final static String RESET_BTN_ID = "Reset"; public final static String ADD_SINGLE_BTN_ID = "Add single item"; public final static String ADD_SINGLE_AT_POS_BTN_ID = "Add single item at pos"; public final static String REMOVE_SINGLE_AT_POS_BTN_ID = "Remove single item at pos"; public final static String REMOVE_POS_EDIT_ID = "Remove at pos"; public final static String ADD_POS_EDIT_ID = "Add at pos"; public final static String MENU_STR = "Menu "; public final static int BUTONS_NUM = 3; VBox root; Label lastSelected; public static void main(String[] args) { launch(args); } protected Scene getScene() { return new MenuBarAppScene(); } @Override public void start(Stage stage) throws Exception { stage.setScene(getScene()); stage.show(); } class BarItem extends Menu { public BarItem(String str) { super(str); setOnAction(new EventHandler() { public void handle(ActionEvent t) { lastSelected.setText(getText()); } }); for (int i = 0; i < BUTONS_NUM; i++) { final MenuItem item = new MenuItem("Item " + i); item.setOnAction(new EventHandler() { public void handle(ActionEvent t) { lastSelected.setText(item.getText()); } }); this.getItems().add(item); } } } public class MenuBarAppScene extends Scene { final MenuBar bar; public MenuBarAppScene() { super(root = new VBox(5)); lastSelected = new Label(); Pane test_pane = new Pane(); test_pane.setId(TEST_PANE_ID); test_pane.setMinSize(600, 300); test_pane.setPrefSize(600, 300); test_pane.setMaxSize(600, 300); root.getChildren().add(test_pane); bar = new MenuBar(); test_pane.getChildren().add(bar); reset(); VBox controls = new VBox(5); root.getChildren().add(controls); Button clear_buton = new Button(CLEAR_BTN_ID) { @Override public void fire() { bar.getMenus().clear(); } }; controls.getChildren().add(clear_buton); Button reset_buton = new Button(RESET_BTN_ID) { @Override public void fire() { reset(); } }; controls.getChildren().add(reset_buton); Button add_buton = new Button(ADD_SINGLE_BTN_ID) { @Override public void fire() { bar.getMenus().add(new BarItem(MENU_STR + bar.getMenus().size())); } }; controls.getChildren().add(add_buton); HBox add_position_box = new HBox(5); controls.getChildren().add(add_position_box); final TextField add_position = new TextField("0"); add_position.setId(ADD_POS_EDIT_ID); Button add_buton_pos = new Button(ADD_SINGLE_AT_POS_BTN_ID) { @Override public void fire() { bar.getMenus().add(Integer.valueOf(add_position.getText()), new BarItem(MENU_STR + bar.getMenus().size())); } }; add_position_box.getChildren().add(add_buton_pos); final Label add_label = new Label("at"); add_position_box.getChildren().add(add_label); add_position_box.getChildren().add(add_position); HBox remove_position_box = new HBox(5); controls.getChildren().add(remove_position_box); final TextField remove_position = new TextField("0"); remove_position.setId(REMOVE_POS_EDIT_ID); Button remove_buton = new Button(REMOVE_SINGLE_AT_POS_BTN_ID) { @Override public void fire() { bar.getMenus().remove(Integer.valueOf(remove_position.getText()).intValue()); } }; remove_position_box.getChildren().add(remove_buton); final Label remove_label = new Label("at"); remove_position_box.getChildren().add(remove_label); remove_position_box.getChildren().add(remove_position); controls.getChildren().add(lastSelected); } protected void reset() { bar.getMenus().clear(); for (int i = 0; i < BUTONS_NUM; i++) { bar.getMenus().add(new BarItem(MENU_STR + i)); } lastSelected.setText(""); } } }