Search code examples
listviewobjectcomboboxjavafxgridpane

How to get object from combobox cell?


I have a ComboBox of objects with GridPane set as Graphic for displaying combobox list. GridPane has 3 columns for image, item's label and a StackPane. I want to click on StackPane and get object that corresponds to this row, but through .getParent().getChildrenUnmodifiable().get(0) I can only get label. If I use .getParent().getParent() that will give me a cell of clicked row. If I'm just changing selection of combobox, I can retrieve object normaly. But how to get object, which stackpane was clicked? enter image description here


Solution

  • Assuming that you have added a mouse click listener to StackPane in your custom ListCell without problem (since this mouse click event may be consumed by combobox row and not be propagated further to stackPane), you can call getItem() in this mouse listener and access the related object of this cell.

    All your layout nodes (GridPane, Stackpane etc.) are renderers of the cell's item. So you cannot access to item (object) by traversing this layout graph.