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

Unable to use Buttons and CheckBoxes within [List|Tree]Cell since Java 8

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Closed
    • Priority: P3
    • Resolution: Duplicate
    • Affects Version/s: 8
    • Fix Version/s: None
    • Component/s: javafx
    • Labels:
      None

      Description

      Hi,

      please consider the following sample code. I am just putting a Button and a CheckBox inside a Cell. (in real project their appearence is determined by the item, therefore it must be done in the updateItem method).

      It seems, that everytime I click the Button or CheckBox, the updateItem method is called again, but the click is never executed on the button/checkbox.

      Is there a workaround? I don't think I am doing something wrong, because the code is pretty simple.


      import javafx.application.Application;
      import javafx.collections.FXCollections;
      import javafx.event.ActionEvent;
      import javafx.event.EventHandler;
      import javafx.scene.Scene;
      import javafx.scene.control.Button;
      import javafx.scene.control.CheckBox;
      import javafx.scene.control.ListCell;
      import javafx.scene.control.ListView;
      import javafx.scene.layout.HBox;
      import javafx.stage.Stage;
      import javafx.util.Callback;


      public class TestApp5 extends Application {
          public static void main(String[] args) {
              launch();
          }

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

              ListView<String> listView = new ListView<String>();
              listView.setItems(FXCollections.<String>observableArrayList("Item 1"));
              listView.setCellFactory(new Callback<ListView<String>, ListCell<String>>() {
                  @Override
                  public ListCell<String> call(ListView<String> stringListView) {
                      ListCell<String> listCell = new ListCell<String>() {
                          @Override
                          protected void updateItem(String item, boolean empty) {
                              super.updateItem(item, empty);

                              setGraphic(null);
                              if (item != null) {
                                  Button button = new Button();
                                  button.setOnAction(new EventHandler<ActionEvent>() {
                                      @Override
                                      public void handle(ActionEvent actionEvent) {
                                          System.out.println("Button clicked");
                                      }
                                  });
                                  HBox hBox = new HBox();
                                  hBox.getChildren().addAll(button, new CheckBox());

                                  setGraphic(hBox);
                              }
                          }
                      };

                      return listCell;
                  }
              });


              Scene scene = new Scene(listView);
              stage.setScene(scene);
              stage.show();
          }
      }

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              jgiles Jonathan Giles
              Reporter:
              cschudtjfx Christian Schudt (Inactive)
              Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved:
                Imported: