繁体   English   中英

如何在JavaFX TableView中将鼠标悬停在某行上?

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

现在,当在tableview中选择一行时,我在鼠标位置显示对话框。 当我将鼠标悬停在每一行上时,我希望显示对话框,似乎有一个CSS:悬停所以我认为它可以在某些容量的Java代码中捕获。

您可以创建一个自定义表行工厂,该工厂向该行的悬停属性添加一个侦听器,并在悬停状态更改时执行操作。

下面是一些示例代码,当用户将鼠标悬停在表行上时,它会更新标签:

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;
});

在示例图像中,鼠标指针(未显示)悬停在Emma Jones的行上,因此标题标题被修改为“地址簿:Emma Jones”

悬停样本

以下是从Oracle JavaFX TableView教程示例代码中采用的完整示例:

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);
        }
    }
} 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM