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

Button events not working in a ComboBox

XMLWordPrintable

    • x86_64
    • generic

      FULL PRODUCT VERSION :
      java version "1.8.0_101"
      Java(TM) SE Runtime Environment (build 1.8.0_101-b13)
      Java HotSpot(TM) 64-Bit Server VM (build 25.101-b13, mixed mode)


      ADDITIONAL OS VERSION INFORMATION :
      x86_64 GNU/Linux Centos, Ubuntu

      A DESCRIPTION OF THE PROBLEM :
      When a Button is placed in a ComboBox's popup list (by setting a cell renderer), its onAction event is never fired when clicked and onMouseClicked very rarely and seemingly randomly. The action event can be triggered by focusing the button with the keyboard (Tab) and hitting space, but not by the mouse.

      If the button is a ToggleButton in a ToggleGroup, the button also does not get selected and the selected item of the toggle group is not changed.

      It does not make a difference if the button is re-created for every call of update item or re-used.

      Just as a note: Clicking the button does not update the list's selection.

      This bug seems very similar to https://bugs.openjdk.java.net/browse/JDK-8092593 , but in a simple ListView, buttons work as expected. The problem only surfaces when a combo box list popup is used.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Create a combobox. Set a cell renderer on the combobox that creates a button for every cell. Click the button and observe that the button's action event is not triggered. If the button is a toggle button, also observe that it never gets selected and the selected toggle in its toggle group is not updated.


      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Clicking the button should trigger the button's action event. If the button is a toggle button, it should become selected and the selected toggle in its toggle group should be updated.
      ACTUAL -
      Clicking the button does not trigger the button's action event. If the button is a toggle button, it never gets selected and the selected toggle in its toggle group is not updated.

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import javafx.application.Application;
      import javafx.scene.Group;
      import javafx.scene.Scene;
      import javafx.scene.control.Button;
      import javafx.scene.control.ComboBox;
      import javafx.scene.control.ContentDisplay;
      import javafx.scene.control.ListCell;
      import javafx.scene.control.ListView;
      import javafx.scene.control.ToggleButton;
      import javafx.scene.control.ToggleGroup;
      import javafx.scene.layout.GridPane;
      import javafx.scene.layout.HBox;
      import javafx.scene.paint.Color;
      import javafx.stage.Stage;
      import javafx.util.Callback;

      public class TestComboBoxToggleButtonsInCells extends Application {

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

          @Override
          public void start(final Stage primaryStage) {
              final Group root = new Group();
              final Scene scene = new Scene(root, 400, 300, Color.WHITE);
              final GridPane gridpane = new GridPane();

              final ToggleGroup toggleGroup = new ToggleGroup();

              final ComboBox<Color> cmb = new ComboBox<>();
              cmb.getItems().addAll(Color.RED, Color.GREEN, Color.BLUE);
              cmb.setCellFactory(new Callback<ListView<Color>, ListCell<Color>>() {
                  @Override
                  public ListCell<Color> call(final ListView<Color> p) {
                      return new ListCell<Color>() {

                          Button b = new Button();
                          ToggleButton tb = new ToggleButton();
                          {
                              System.out.println("new list cell " + toString());
                              setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
                              tb.setOnAction(evt -> System.out.println("action on toggle button " + tb.getText()));
                              b.setOnAction(evt -> System.out.println("action on button " + tb.getText()));
                              tb.setToggleGroup(toggleGroup);
                          }

                          @Override
                          protected void updateItem(final Color item, final boolean empty) {
                              super.updateItem(item, empty);
                              System.out.println("update item on " + toString() + " with item=" + item + " , empty=" + empty);

                              if (item == null || empty) {
                                  setGraphic(null);
                              } else {
                                  tb.setText(item.toString());
                                  b.setText(item.toString());
                                  setGraphic(new HBox(b, tb));
                              }
                          }
                      };
                  }
              });
              cmb.getSelectionModel().selectedItemProperty()
                      .addListener(obs -> cmb.setButtonCell(cmb.getCellFactory().call(null)));

              toggleGroup.selectedToggleProperty()
                      .addListener(obs -> System.out.println(toggleGroup.getSelectedToggle() + " toggled"));

              gridpane.add(cmb, 2, 0);

              root.getChildren().add(gridpane);
              primaryStage.setScene(scene);

              primaryStage.show();
          }

      }
      ---------- END SOURCE ----------

            Unassigned Unassigned
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated: