How to add a mouse doubleclick event listener to the cells of a ListView in javafx?

Krishna picture Krishna · Mar 20, 2014 · Viewed 23.2k times · Source

I have a list of links in a ListView. I want to add an mouseEventListener to each cell of the list so that whenever a user double clicks the list item link is opened. I can write the functionality of opening the link on my own but I am not able to add the doubleclick event with every cell in the list. Please help...

Answer

ItachiUchiha picture ItachiUchiha · Mar 21, 2014

Let us consider your ListView as playList. Now you can implement the mouse listener with double click functionality on each cell using

playList.setOnMouseClicked(new EventHandler<MouseEvent>() {

    @Override
    public void handle(MouseEvent click) {

        if (click.getClickCount() == 2) {
           //Use ListView's getSelected Item
           currentItemSelected = playList.getSelectionModel()
                                                    .getSelectedItem();
           //use this to do whatever you want to. Open Link etc.
        }
    }
});