/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package menubugs; import javafx.scene.input.MouseEvent; import javafx.event.EventHandler; import javafx.stage.Stage; import javafx.scene.Scene; import javafx.scene.Group; import javafx.scene.control.MenuBar; import javafx.scene.control.Menu; import javafx.scene.control.MenuItem; import javafx.application.Application; import javafx.scene.control.RadioButton; import javafx.scene.layout.VBox; import javafx.scene.control.ToggleGroup; public class AddMenuItemTest extends Application { public static void main(String[] args) { Application.launch(args); } RadioButton RemoveMenuItem = new RadioButton(); RadioButton addMenuItem = new RadioButton(); Menu fileMenu; Menu editMenu; MenuItem fileMenuItem = new MenuItem("MenuItem 4"); @Override public void start(Stage stage) { Scene scene; ToggleGroup toggleGroup = new ToggleGroup(); addMenuItem.setToggleGroup(toggleGroup); addMenuItem.setText("Add MenuItem"); addMenuItem.setSelected(false); addMenuItem.setOnMouseClicked(new EventHandler() { @Override public void handle(MouseEvent me) { if (addMenuItem.isSelected()) { fileMenu.getItems().add(fileMenuItem); } } }); RemoveMenuItem.setToggleGroup(toggleGroup); RemoveMenuItem.setText("Remove MenuItem"); RemoveMenuItem.setSelected(false); RemoveMenuItem.setOnMouseClicked(new EventHandler() { @Override public void handle(MouseEvent me) { int i2 = 0; if (RemoveMenuItem.isSelected()) { i2 = fileMenu.getItems().indexOf(fileMenuItem); } if (i2 != -1) { fileMenu.getItems().remove(i2); } } }); VBox box = new VBox(10.0F); box.setTranslateX(30.0F); box.setTranslateY(50.0F); box.getChildren().clear(); box.getChildren().addAll(addMenuItem, RemoveMenuItem); MenuBar menuBar = new MenuBar(); fileMenu = new Menu("File"); menuBar.getMenus().clear(); menuBar.getMenus().addAll(fileMenu); for (int i = 1; i <= 3; i++) { MenuItem menuitem = new MenuItem(); menuitem.setText("MenuItem-" + i); fileMenu.getItems().add(menuitem); } scene = new Scene(new Group(), 400, 400); ((Group) scene.getRoot()).getChildren().clear(); ((Group) scene.getRoot()).getChildren().addAll(menuBar, box); stage.setScene(scene); stage.setVisible(true); } }