Adding hover listener to cells of a specific column in javafx table

Viewed 1858

I want to create a OnMouseOver Listener to cells of a specific column of a TableView, such that a popup window should appeared with some message, when I hover to it's cells. This is how I am creating a table and its columns.

private TableView<Stock> stockTable = new TableView<>();
ObservableList columns = stockTable.getColumns();
public TableColumn createTextColumn(String columnName, String columnCaption,BooleanProperty editingStarted) {
    TableColumn column = new TableColumn(columnCaption);
    column.setCellValueFactory(new PropertyValueFactory<>(columnName));
    column.setCellFactory(TextFieldTableCell.forTableColumn());
    return column;
}
final TableColumn nameColumn 
                         = createTextColumn("name", "Product Name", editingStarted);
columns.add(nameColumn);

So is it possible to do it. I know how to create listeners for rows. But I found nothing on internet on listening to cells of table. Thanks in advance for any help.

2 Answers

The above example also displays the label when it's empty. This small change will fix this ...

                    if (event.getEventType() == MouseEvent.MOUSE_EXITED) {
                        popup.hide();
                    } else if (event.getEventType() == MouseEvent.MOUSE_ENTERED) {
                        if(result.getText() != null) {
                            if (!"".equals(result.getText())) {
                                popup.show(result, event.getScreenX() + 10, event.getScreenY());
                            }
                        }
                    }
Related