如何在JavaFX TableView中悬停一行时发生一些事情? [英] How do I make something happen on hover of a row in a JavaFX TableView?

查看:165
本文介绍了如何在JavaFX TableView中悬停一行时发生一些事情?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

推荐答案

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

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

在示例图像中,鼠标指针(未显示)悬停在Emma Jones的行上,所以标题标题被修改为地址簿:艾玛琼斯

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"

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

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

这篇关于如何在JavaFX TableView中悬停一行时发生一些事情?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆