If a control, like a button is inside a combobox by using a cellfactory, is clicked. It doesn't respond to the events. So, if we have an scenario like the following, events won´t be triggered. There is no possibility to develop complex cells that could consume the mouse events.
public class ComboBoxWithCellFactory extends Application {
@Override
public void start(Stage primaryStage) {
ComboBox btn = new ComboBox();
btn.setCellFactory(
new Callback<ListView<String>, ListCell<String>>() {
@Override
public ListCell<String> call(ListView<String> param) {
final ListCell<String> cell = new ListCell<String>() {
{
super.setPrefWidth(100);
}
@Override
public void updateItem(String item, boolean empty) {
super.updateItem(item, empty);
if (item != null) {
setText(item);
Button btn = new Button("hola");
btn.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
throw new UnsupportedOperationException("Not supported yet.");
}
});
btn.setOnMouseClicked(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent event) {
throw new UnsupportedOperationException("Not supported yet.");
}
});
setGraphic(btn);
} else {
setText(null);
}
}
};
return cell;
}
});
btn.getItems().add("mundo");
StackPane root = new StackPane();
root.getChildren().add(btn);
Scene scene = new Scene(root, 300, 250);
primaryStage.setTitle("Hola mundo!");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
public class ComboBoxWithCellFactory extends Application {
@Override
public void start(Stage primaryStage) {
ComboBox btn = new ComboBox();
btn.setCellFactory(
new Callback<ListView<String>, ListCell<String>>() {
@Override
public ListCell<String> call(ListView<String> param) {
final ListCell<String> cell = new ListCell<String>() {
{
super.setPrefWidth(100);
}
@Override
public void updateItem(String item, boolean empty) {
super.updateItem(item, empty);
if (item != null) {
setText(item);
Button btn = new Button("hola");
btn.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
throw new UnsupportedOperationException("Not supported yet.");
}
});
btn.setOnMouseClicked(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent event) {
throw new UnsupportedOperationException("Not supported yet.");
}
});
setGraphic(btn);
} else {
setText(null);
}
}
};
return cell;
}
});
btn.getItems().add("mundo");
StackPane root = new StackPane();
root.getChildren().add(btn);
Scene scene = new Scene(root, 300, 250);
primaryStage.setTitle("Hola mundo!");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}