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

Changing a graphic on a MenuItem when it is clicked on causes an infinite loop



    • Type: Bug
    • Status: Resolved
    • Priority: P3
    • Resolution: Fixed
    • Affects Version/s: 8
    • Fix Version/s: emb-8u6, 8u20
    • Component/s: javafx
    • Environment:

      Windows and Linux with Java 1.8.0ea b124


      If you change the graphic when a MenuItem is invoked with the mouse you can instigate an infinite loop. It looks like there is code in ContextMenuContainer that creates new child nodes whenever the graphic on a menuitem changes (see handlePropertyChanged(String) and createChildren()). This code adds a new MouseListener to the new children and never removes the old listener.

      Run the provided test class and use your mouse to click on the MenuItem in the "Test" Menu. It will alter the label in the stage and its own appearance. If you keep toggling the MenuItem you will see in the console that the number of action events being fired increases each time.

      If the code that alters the graphic is not in a "runLater" it will cause an infinite loop as the new child nodes register their mouse listener and then receive the same mouse event.

      ************************* Test Class *********************************************
      import javafx.application.Application;
      import javafx.application.Platform;
      import javafx.event.ActionEvent;
      import javafx.event.EventHandler;
      import javafx.scene.Scene;
      import javafx.scene.control.Label;
      import javafx.scene.control.Menu;
      import javafx.scene.control.MenuBar;
      import javafx.scene.control.MenuItem;
      import javafx.scene.layout.BorderPane;
      import javafx.scene.paint.Color;
      import javafx.scene.shape.Circle;
      import javafx.stage.Stage;

      public class MenuTest extends Application {

         @Override public void start(final Stage primaryStage) throws Exception {


            final String labelOnString = "Feature On";
            final String labelOffString = "Feature Off";
            final Label label = new Label(labelOffString);
            final Circle onCircle = new Circle(10, Color.RED);
            final Circle offCircle = new Circle(10, Color.GREEN);
            final String on = "Turn Off";
            final String off = "Turn On";
            final MenuItem testItem = new MenuItem(off, offCircle);
            testItem.setOnAction(new EventHandler<ActionEvent>() {
               @Override public void handle(ActionEvent event) {
                  System.out.println("firing menu item: " + testItem.getText());
                  if ( testItem.getText().equals(on) ) {
                     Platform.runLater(new Runnable() {
                        @Override public void run() {
                           // comment out the graphic change to see normal behaviour
                  } else {
                     Platform.runLater(new Runnable() {
                        @Override public void run() {
                           // comment out the graphic change to see normal behaviour
            Menu menu = new Menu("Test");
            MenuBar menuBar = new MenuBar();
                  new Scene( new BorderPane(label, menuBar, null, null , null) ) );


         public static void main(String[] args) throws Exception {





            jgiles Jonathan Giles
            csmithjfx Charles Smith (Inactive)
            1 Vote for this issue
            3 Start watching this issue