Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Unable to use Buttons/CheckBoxes in List/TreeCell since JavaFX 8

cshMar 25 2014 — edited Apr 17 2014

Hi,

I've already filed a bug for this, but I want to also ask here for a workaround, since it's quite critical for us and probably the only issue, which is preventing us from migrating to Java 8.

The problem:

Since Java 8 we can't use ButtonBase elements in ListCells and TreeCells. E.g. when the cell's graphic has a Button or CheckBox and I click it, nothing happens. Instead the updateItem method is called again and renders again.

Is there any workaround or anything I am doing wrong? It worked well with JavaFX 2.2.

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();

     }

}

Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on May 15 2014
Added on Mar 25 2014
13 comments
6,321 views