'Detect doubleclick on row of TableView JavaFX

I need to detect double clicks on a row of a TableView.

How can I listen for double clicks on any part of the row and get all data of this row to print it to the console?



Solution 1:[1]

Example:

table.setOnMousePressed(new EventHandler<MouseEvent>() {
    @Override 
    public void handle(MouseEvent event) {
        if (event.isPrimaryButtonDown() && event.getClickCount() == 2) {
            System.out.println(table.getSelectionModel().getSelectedItem());                   
        }
    }
});

If you are using custom selection model, then you can get the row from event, example:

table.setOnMousePressed(new EventHandler<MouseEvent>() {
    @Override 
    public void handle(MouseEvent event) {
        if (event.isPrimaryButtonDown() && event.getClickCount() == 2) {
            Node node = ((Node) event.getTarget()).getParent();
            TableRow row;
            if (node instanceof TableRow) {
                row = (TableRow) node;
            } else {
                // clicking on text part
                row = (TableRow) node.getParent();
            }
            System.out.println(row.getItem());
        }
    }
});

Solution 2:[2]

This works for me:

table.setOnMouseClicked((MouseEvent event) -> {
            if (event.getButton().equals(MouseButton.PRIMARY) && event.getClickCount() == 2){
                System.out.println(table.getSelectionModel().getSelectedItem());
            }
        });
    }

Solution 3:[3]

If you are using SceneBuilder you can set your table's OnMouseClicked to handleRowSelect() method as shown below:

MyType temp;
Date lastClickTime;
@FXML
private void handleRowSelect() {
    MyType row = myTableView.getSelectionModel().getSelectedItem();
    if (row == null) return;
    if(row != temp){
        temp = row;
        lastClickTime = new Date();
    } else if(row == temp) {
        Date now = new Date();
        long diff = now.getTime() - lastClickTime.getTime();
        if (diff < 300){ //another click registered in 300 millis
             System.out.println("Edit dialog");
        } else {
            lastClickTime = new Date();
        }
    }
}

Solution 4:[4]

This answer has been tested:

table.setOnMouseClicked( event -> {
   if( event.getClickCount() == 2 ) {
      System.out.println( table.getSelectionModel().getSelectedItem());
   }});

table.getSelectionModel().getSelectedItem() can be use since we catch a double-click. One the first click the selection moves, on the second this handler is executed.

Solution 5:[5]

I had similar situation not to detect mouse double click event on TableView. Above all samples worked perfectly. but my application did not detect double click event at all.

But I found that if TableView is on editable, mouse double click event can not be detected !!

check your application if TableView is on editable like this.

tableView.setEditable( true );

if then, double click event only raises on same row selected.

Solution 6:[6]

Extending the previous answer:

The extra check ensures the selected row was double clicked - ignoring double clicks on empty rows or the column header

table.setRowFactory(param -> {
            TableRow<MyType> row = new TableRow<>();
            row.setOnMouseClicked(event -> Optional.ofNullable(row.getItem()).ifPresent(rowData-> {
                if(event.getClickCount() == 2 && rowData.equals(table.getSelectionModel().getSelectedItem())){
                    System.out.println(rowData);
                }
            }));
            return row;
        });
```

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Abdul.Moqueet
Solution 3 Neuron
Solution 4 Aubin
Solution 5 nayasis
Solution 6 Anthony Stewart