How do I make something happen on hover of a row in a JavaFX TableView?


Question

Right now I have dialog show up at the mouse position when an row was selected in the tableview. I'm looking to have the dialog show up when I hover over each row, there seems to be a CSS :hover so I assume it can be caught in java code in some capacity.

1
2
10/9/2014 3:41:14 AM

Accepted Answer

You can create a custom table row factory which adds a listener to the hover property of the row and takes action when the hover status changes.

Here is some sample code which updates a label as the user hovers over table rows:

table.setRowFactory(tableView -> {
    final TableRow<Person> row = new TableRow<>();

    row.hoverProperty().addListener((observable) -> {
        final Person person = row.getItem();

        if (row.isHover() && person != null) {
            label.setText("Address Book: " 
                + person.getFirstName() + " " 
                + person.getLastName()
            );
        } else {
            label.setText("Address Book");
        }
    });

    return row;
});

In the sample image the mouse pointer (not shown) is hovered over the row for Emma Jones, so the title header is modified to read "Address Book: Emma Jones"

hover sample

Here is a complete sample adopted from the Oracle JavaFX TableView tutorial sample code:

import javafx.application.Application;
import javafx.beans.property.SimpleStringProperty;
import javafx.collections.*;
import javafx.geometry.Insets;
import javafx.scene.*;
import javafx.scene.control.*;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.stage.Stage;

public class TableViewSample extends Application {

    private TableView<Person> table = new TableView<Person>();
    private final ObservableList<Person> data =
        FXCollections.observableArrayList(
            new Person("Jacob", "Smith", "jacob.smith@example.com"),
            new Person("Isabella", "Johnson", "isabella.johnson@example.com"),
            new Person("Ethan", "Williams", "ethan.williams@example.com"),
            new Person("Emma", "Jones", "emma.jones@example.com"),
            new Person("Michael", "Brown", "michael.brown@example.com")
        );

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage stage) {
        final Label label = new Label("Address Book");
        label.setFont(new Font("Arial", 20));

        table.setEditable(true);

        TableColumn<Person, String> firstNameCol = new TableColumn<>("First Name");
        firstNameCol.setMinWidth(100);
        firstNameCol.setCellValueFactory(
                new PropertyValueFactory<>("firstName"));

        TableColumn<Person, String> lastNameCol = new TableColumn<>("Last Name");
        lastNameCol.setMinWidth(100);
        lastNameCol.setCellValueFactory(
                new PropertyValueFactory<>("lastName"));

        TableColumn<Person, String> emailCol = new TableColumn<>("Email");
        emailCol.setMinWidth(200);
        emailCol.setCellValueFactory(
                new PropertyValueFactory<>("email"));

        table.setRowFactory(tableView -> {
            final TableRow<Person> row = new TableRow<>();
            row.hoverProperty().addListener((observable) -> {
                final Person person = row.getItem();
                if (row.isHover() && person != null) {
                    label.setText(
                       "Address Book: "
                               + person.getFirstName() + " "
                               + person.getLastName()
                    );
                } else {
                    label.setText("Address Book");
                }
            });

            return row;
        });

        table.setItems(data);
        table.getColumns().addAll(firstNameCol, lastNameCol, emailCol);

        final VBox vbox = new VBox(10);
        vbox.setPadding(new Insets(10));
        vbox.getChildren().addAll(label, table);

        Scene scene = new Scene(vbox);
        stage.setScene(scene);
        stage.show();
    }

    public static class Person {

        private final SimpleStringProperty firstName;
        private final SimpleStringProperty lastName;
        private final SimpleStringProperty email;

        private Person(String fName, String lName, String email) {
            this.firstName = new SimpleStringProperty(fName);
            this.lastName = new SimpleStringProperty(lName);
            this.email = new SimpleStringProperty(email);
        }

        public String getFirstName() {
            return firstName.get();
        }

        public void setFirstName(String fName) {
            firstName.set(fName);
        }

        public String getLastName() {
            return lastName.get();
        }

        public void setLastName(String fName) {
            lastName.set(fName);
        }

        public String getEmail() {
            return email.get();
        }

        public void setEmail(String fName) {
            email.set(fName);
        }
    }
} 
7
10/9/2014 8:51:25 PM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon