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

label.setGraphic() with an ImageView does not respond to mouse clicks

    XMLWordPrintable

Details

    • Bug
    • Resolution: Unresolved
    • P4
    • tbd
    • 7u15
    • javafx
    • java.version: 1.7.0_17, javafx.runtime.version: 2.2.7-b01, java.version: 1.8.0-ea, javafx.runtime.version: 8.0.0-ea-b82 problems seems to occur on both Mac OS X and Ubuntu

    Description

      When we add an onMouseClicked-Eventhandler to an ImageView and set this ImageView as an icon to a Label with the setGraphic() method, the ImageView will not respond to any mouse clicked events.

      However, when we wrap the ImageView in an HBox the mouse-event works! If we use a Rectangle node in place of the ImageView mouse-events work as well (without the need to wrap in an HBox).

      ImageView and Rectangle appear as regular nodes to us. So why is there a difference in mouse-event processing?

      SSCCE (you'll need a "any.png" image resource in the classpath)

      import javafx.application.Application;
      import javafx.event.EventHandler;
      import javafx.geometry.Insets;
      import javafx.scene.Node;
      import javafx.scene.Scene;
      import javafx.scene.control.Label;
      import javafx.scene.image.Image;
      import javafx.scene.image.ImageView;
      import javafx.scene.input.MouseEvent;
      import javafx.scene.layout.HBox;
      import javafx.scene.layout.VBox;
      import javafx.stage.Stage;

      public class ImageViewApp extends Application {
          private final Image image = new Image(ImageViewApp.class.getResourceAsStream("any.png"));

          @Override
          public void start(final Stage stage) throws Exception {
              System.out.println("java.version: " + System.getProperty("java.version"));
              System.out.println("javafx.runtime.version: " + System.getProperty("javafx.runtime.version"));

              // create a label and call setGraphic() with an ImageView node
              Label label1 = new Label("onMouseClick on icon does not work");
              label1.setGraphic(createImageView());

              // create a label and call setGraphic() with an ImageView node wrapped in an HBox node
              Label label2 = new Label("onMouseClick on icon WORKS");
              label2.setGraphic(wrapInHBox(createImageView()));

              // put everything into an VBox
              VBox vbox = new VBox();
              vbox.setPadding(new Insets(20));
              vbox.setSpacing(20);
              vbox.getChildren().addAll(label1, label2);

              stage.setScene(new Scene(vbox));
              stage.show();
          }

          private ImageView createImageView() {
              ImageView imageView = new ImageView(image);
              imageView.setOnMouseClicked(new EventHandler<MouseEvent>() {
                  @Override
                  public void handle(final MouseEvent mouseEvent) {
                      System.out.println("CLICKED"); // will only be seen when the ImageView is wrapped in a HBox
                  }
              });
              return imageView;
          }

          private Node wrapInHBox(final ImageView imageView) {
              HBox result = new HBox();
              result.getChildren().add(imageView);
              return result;
          }

          public static void main(final String[] args) {
              launch(args);
          }
      }

      Attachments

        Activity

          People

            Unassigned Unassigned
            jhohmuthjfx Jens Hohmuth (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Imported: