-
Bug
-
Resolution: Fixed
-
P3
-
8
-
OS X 10.9, JDK 8 RC 64bit
When a Button is placed in a ListCell (or probably any other Cell) its onAction event is never fired and onMouseClicked very rarely and seemingly randomly. Found on JDK8 RC. Here is some example code:
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ListCell;
import javafx.scene.control.ListView;
import javafx.stage.Stage;
public class Testing extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
ObservableList<String> strings = FXCollections.observableArrayList();
strings.addAll("text", "another");
ListView<String> list = new ListView<>(strings);
list.setCellFactory(cell -> new ButtonCell());
Scene s = new Scene(list, 300, 400);
primaryStage.setScene(s);
primaryStage.show();
}
private class ButtonCell extends ListCell<String> {
@Override
protected void updateItem(String item, boolean empty) {
super.updateItem(item, empty);
if (empty) {
setText(null);
setGraphic(null);
} else {
setText(item);
Button b = new Button("button");
b.setOnAction(action -> System.out.println("zam"));
b.setOnMouseClicked(action -> System.out.println("bam"));
setGraphic(b);
}
}
}
}
WORKAROUND... return from updateItem if the cell's index has not changed.
private class ButtonCell extends ListCell<String> {
int currentIndex = -1;
@Override
protected void updateItem(String item, boolean empty) {
super.updateItem(item, empty);
if (empty) {
setText(null);
setGraphic(null);
} else {
int index = getIndex();
if (currentIndex == index) return;
currentIndex = index;
setText(item);
Button b = new Button("button");
b.setOnAction(action -> System.out.println("zam"));
b.setOnMouseClicked(action -> System.out.println("bam"));
setGraphic(b);
}
}
}
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ListCell;
import javafx.scene.control.ListView;
import javafx.stage.Stage;
public class Testing extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
ObservableList<String> strings = FXCollections.observableArrayList();
strings.addAll("text", "another");
ListView<String> list = new ListView<>(strings);
list.setCellFactory(cell -> new ButtonCell());
Scene s = new Scene(list, 300, 400);
primaryStage.setScene(s);
primaryStage.show();
}
private class ButtonCell extends ListCell<String> {
@Override
protected void updateItem(String item, boolean empty) {
super.updateItem(item, empty);
if (empty) {
setText(null);
setGraphic(null);
} else {
setText(item);
Button b = new Button("button");
b.setOnAction(action -> System.out.println("zam"));
b.setOnMouseClicked(action -> System.out.println("bam"));
setGraphic(b);
}
}
}
}
WORKAROUND... return from updateItem if the cell's index has not changed.
private class ButtonCell extends ListCell<String> {
int currentIndex = -1;
@Override
protected void updateItem(String item, boolean empty) {
super.updateItem(item, empty);
if (empty) {
setText(null);
setGraphic(null);
} else {
int index = getIndex();
if (currentIndex == index) return;
currentIndex = index;
setText(item);
Button b = new Button("button");
b.setOnAction(action -> System.out.println("zam"));
b.setOnMouseClicked(action -> System.out.println("bam"));
setGraphic(b);
}
}
}
- duplicates
-
JDK-8094133 Buttons in ListView
- Closed
-
JDK-8095749 Unable to use Buttons and CheckBoxes within [List|Tree]Cell since Java 8
- Closed
- relates to
-
JDK-8183444 Button events not working in a ComboBox
- Open
-
JDK-8092589 TableHeaders behave badly when horizontal scrollbar is visible
- Resolved
-
JDK-8095338 [ListView] ListView not downsizing cells when its breadth shrinks
- Resolved