import com.sun.javafx.runtime.VersionInfo; import javafx.application.Application; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.geometry.Side; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.control.CheckBox; import javafx.scene.control.ContextMenu; import javafx.scene.control.Label; import javafx.scene.control.MenuItem; import javafx.scene.control.TextField; import javafx.scene.input.MouseEvent; import javafx.scene.layout.HBox; import javafx.scene.layout.Pane; import javafx.scene.layout.VBox; import javafx.stage.Stage; import javafx.stage.WindowEvent; public class ContextMenuBug 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 LAST_SELECTED_ID = "Last selected"; public final static String LAST_SELECTED_ACTION_ID = "Last selected action"; public final static String SHOWN_CHECK_ID = "Shown"; public final static String ERROR_ID = "Error"; public final static String SHOWING_CHECK_ID = "Showing"; public final static String MENU_ITEM = "Menu item "; public final static int BUTONS_NUM = 3; public final static String MENU_ID = "Menu"; VBox root; Label last_selected; Label last_selected_action; public static ContextMenu menu; public static void main(String[] args) { launch(args); } protected Scene getScene() { return new MenuBarAppScene(); } @Override public void start(Stage stage) throws Exception { stage.setTitle(VersionInfo.getRuntimeVersion()); stage.setScene(getScene()); stage.show(); } class ActionItem extends MenuItem { public ActionItem(String str) { super(str); setOnAction(new EventHandler() { public void handle(ActionEvent t) { last_selected.setText(getText()); } }); } } public class MenuBarAppScene extends Scene { public MenuBarAppScene() { super(root = new VBox(5)); Pane test_pane = new Pane(); test_pane.setId(TEST_PANE_ID); test_pane.setPrefSize(600, 300); test_pane.setMaxSize(600, 300); root.getChildren().add(test_pane); menu = new ContextMenu(); final Label menu_label = new Label(MENU_ID); menu_label.setId(MENU_ID); menu_label.setOnMouseReleased(new EventHandler() { public void handle(MouseEvent t) { if (menu.isShowing()) { System.out.println("hide"); menu.hide(); } else { System.out.println("show"); menu.show(menu_label, Side.BOTTOM, 0, 0); } } }); test_pane.getChildren().add(menu_label); reset(); VBox controls = new VBox(5); root.getChildren().add(controls); Button clear_buton = new Button(CLEAR_BTN_ID) { @Override public void fire() { menu.getItems().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() { menu.getItems().add(new ActionItem(MENU_ITEM + menu.getItems().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() { menu.getItems().add(Integer.valueOf(add_position.getText()), new ActionItem(MENU_ITEM + menu.getItems().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() { menu.getItems().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); HBox last_selected_box = new HBox(5); controls.getChildren().add(last_selected_box); Label prompt_last_selected = new Label(LAST_SELECTED_ID); last_selected = new Label(); last_selected.setId(LAST_SELECTED_ID); last_selected_box.getChildren().add(prompt_last_selected); last_selected_box.getChildren().add(last_selected); Label prompt_shown = new Label(SHOWN_CHECK_ID); final CheckBox shown = new CheckBox(); shown.setId(SHOWN_CHECK_ID); menu.setOnShown(new EventHandler() { public void handle(WindowEvent t) { shown.setSelected(true); } }); menu.setOnHidden(new EventHandler() { public void handle(WindowEvent t) { shown.setSelected(false); } }); last_selected_box.getChildren().add(prompt_shown); last_selected_box.getChildren().add(shown); Label prompt_showing = new Label(SHOWING_CHECK_ID); final CheckBox showing = new CheckBox(); showing.setId(SHOWING_CHECK_ID); menu.setOnShowing(new EventHandler() { public void handle(WindowEvent t) { showing.setSelected(true); } }); menu.setOnHiding(new EventHandler() { public void handle(WindowEvent t) { showing.setSelected(false); } }); last_selected_box.getChildren().add(prompt_showing); last_selected_box.getChildren().add(showing); HBox last_selected_action_box = new HBox(5); controls.getChildren().add(last_selected_action_box); Label prompt_last_selected_action = new Label(LAST_SELECTED_ACTION_ID); last_selected_action = new Label(); last_selected_action.setId(LAST_SELECTED_ACTION_ID); last_selected_action_box.getChildren().add(prompt_last_selected_action); last_selected_action_box.getChildren().add(last_selected_action); Label error = new Label(); error.setId(ERROR_ID); controls.getChildren().add(error); EventHandler handler = new EventHandler() { public void handle(ActionEvent t) { last_selected_action.setText(((MenuItem)t.getTarget()).getText()); } }; menu.setOnAction(handler); if (menu.getOnAction() != handler) { error.setText("menu.setOnAction() fails"); } } protected void reset() { menu.getItems().clear(); for (int i = 0; i < BUTONS_NUM; i++) { menu.getItems().add(new ActionItem(MENU_ITEM + i)); } } } }