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();
}
}
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();
}
}
- duplicates
-
JDK-8092593 Button's events are not working in a ListView
-
- Resolved
-