I'd like to know if there's possible to add an Image Button after every ListView Item. For example:
where those red squares should have a button instead. If it's possible, how can I handle item's button's click event?
EDIT: if there's another control that can do it, please let me know. I'm testing TableView.
Thanks in advance!
The previous solution worked fine when my list was not big and I didn't add more Controls making the Node more complex. When I added more Controls I found a problem with concurrency, maybe it is also because I am drawing maps that have more than 12000 objects (lines and polygons). The problem (what I could see) was that some of the items in ListView will be repeated internally meaning that cellfactory will be created twice and/or not created. It seems "updateItem" is last in the queue of the thread. So to work around the problem I have to create the node before creating the ListView. Something like this:
I used the simple DefaultListCell recommended in fxexperience.com
I was testing this recently. My solution:
The cells will look like this:
The relevant part is the
XCell.updateItem
method and thesetGraphic
call. WithsetGraphic()
usually an icon shall be set for a label, but it works equally well to set a complex Node - in this case the HBox with label and button.You need to make sure, that the Button's event handler refers to the correct item in the list. In the first link below, it is mentioned, that the currently selected item might be sufficient for now. So fetch the currently selected index in the list when handling the button's event.
You might like to look at these:
You can also use List of custom HBox objects. Example below shows the use of such custom HBox inner class and process of nesting list of such objects into ListView control.
The result of this implementation will look like this:
However, in order to efficacy manage Buttons events, you should consider in adding additional custom Button object into HBoxCustom constructor method, and create a proper method, which will allow you to control buttons click-events.