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

Buttons in ListView

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Resolved
    • Priority: P3
    • Resolution: Duplicate
    • Affects Version/s: 8
    • Fix Version/s: 8u20
    • Component/s: javafx
    • Labels:
    • Environment:

      Windows 7, Java 1.8.0, JavaFX 8.0.0

      Description

      I have a problem in JavaFX 8 that neither onAction handler nor onMouseClicked handler are triggered when I click on a Button which are included in a ListView (ListCells). In JavaFX 2.X this works OK (means both handlers are triggered).

      Here is a code sample:

       

          import javafx.application.Application;
          import javafx.collections.FXCollections;
          import javafx.collections.ObservableList;
          import javafx.event.ActionEvent;
          import javafx.event.EventHandler;
          import javafx.scene.Scene;
          import javafx.scene.control.Button;
          import javafx.scene.control.ListCell;
          import javafx.scene.control.ListView;
          import javafx.scene.layout.Pane;
          import javafx.scene.layout.VBox;
          import javafx.stage.Stage;
          import javafx.util.Callback;
          public class ButtonInList extends Application {
              @Override
              public void start(Stage primaryStage) {
                  ObservableList<String> items = FXCollections.observableArrayList();;
                  items.addAll(
                          "Adam", "Alex", "Alfred", "Albert"
                          );
                  final ListView<String> listView = new ListView<String>(items);
                  listView.setCellFactory(new Callback<ListView<String>,
                          ListCell<String>>() {
                              @Override
                              public ListCell<String> call(ListView<String> list) {
                                  return new ButtonCell();
                              }
                          });
                  Button refButton = new Button("Reference");
                  refButton.setOnAction(new EventHandler<ActionEvent>() {
                      @Override
                      public void handle(ActionEvent arg0) {
                          System.out.println("Reference Button Handler");
                      }
                  });
                  Pane root = new VBox();
                  root.getChildren().addAll(refButton, listView);
                  primaryStage.setScene(new Scene(root, 200, 250));
                  primaryStage.show();
              }
              static class ButtonCell extends ListCell<String> {
                  @Override
                  public void updateItem(final String item, boolean empty) {
                      super.updateItem(item, empty);
                      if (item != null) {
                          Button button = new Button(item);
                          button.setOnAction(new EventHandler<ActionEvent>() {
                              @Override
                              public void handle(ActionEvent arg0) {
                                  System.out.println(item + " Button Handler");
                              }
                          });
                          setGraphic(button);
                      }
                  }
              }
              public static void main(String[] args) {
                  launch(args);
              }
          }

       

      Reference button prints correctly "Reference Button Handler", but buttons in ListView don't print anything.

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              jgiles Jonathan Giles
              Reporter:
              duke J. Duke (Inactive)
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved:
                Imported: