Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8124669

Unable to add/remove MenuItems in (Context)Menu after it is shown (Regression!)



      We asynchronously populate a ContextMenu using the Service class.

      While the service is running we display a dummy MenuItem with a progress indicator.
      When the service is finished we populate the Context Menu with the service result.

      This worked fine in 2.2.

      With 8.0 b84, I am unable to remove the MenuItem with the progress indicator and I am also unable to add new items to the context menu.

      See this example:

      import javafx.application.Application;
      import javafx.beans.value.ChangeListener;
      import javafx.beans.value.ObservableValue;
      import javafx.concurrent.Service;
      import javafx.concurrent.Task;
      import javafx.concurrent.Worker;
      import javafx.event.EventHandler;
      import javafx.scene.Scene;
      import javafx.scene.control.ContextMenu;
      import javafx.scene.control.Label;
      import javafx.scene.control.MenuItem;
      import javafx.scene.control.ProgressIndicator;
      import javafx.scene.layout.VBox;
      import javafx.stage.Stage;
      import javafx.stage.WindowEvent;

      public class TestApp5 extends Application {

          public static void main(String[] args) {

          public void start(final Stage stage) throws Exception {

              VBox root = new VBox();

              Label label = new Label("ContextMenu");
              label.setContextMenu(new MyContextMenu());


              Scene scene = new Scene(root);

          private class MyContextMenu extends ContextMenu {

              private MyService service;

              public MyContextMenu() {

                  final MenuItem progressItem = new MenuItem();
                  progressItem.setGraphic(new ProgressIndicator());

                  service = new MyService();
                  service.stateProperty().addListener(new ChangeListener<Worker.State>() {
                      public void changed(ObservableValue<? extends Worker.State> observableValue, Worker.State state, Worker.State state1) {
                          switch (state1) {
                              case SCHEDULED:
                                  System.out.println("adding progress item");
                              case SUCCEEDED:
                                  System.out.println("removing progress item");
                                  System.out.println("adding new item");
                                  getItems().add(new MenuItem("new")); // based on service result.

                  MenuItem menuItem1 = new MenuItem("First Menu Item");
                  MenuItem menuItem2 = new MenuItem("Last Menu Item");

                  setOnShowing(new EventHandler<WindowEvent>() {
                      public void handle(WindowEvent windowEvent) {

                  getItems().addAll(menuItem1, menuItem2);

          private class MyService extends Service<Void> {

              protected Task<Void> createTask() {
                  return new Task<Void>() {
                      protected Void call() throws Exception {
                          return null;




            • Assignee:
              psomashe Parvathi Somashekar (Inactive)
              cschudtjfx Christian Schudt (Inactive)
            • Votes:
              0 Vote for this issue
              3 Start watching this issue


              • Due: