在表格视图中的特定行位置显示JavaFx中的工具提示

在表格视图中的特定行位置显示JavaFx中的工具提示,javafx,position,tooltip,row,tableview,Javafx,Position,Tooltip,Row,Tableview,如何为TableView的每一行显示工具提示 例如: 当我把鼠标放在一个特定的表行上时,它会给我该行数据的详细信息 感谢您使用行工厂,并将工具提示添加到表格行: import javafx.application.Application; import javafx.beans.property.SimpleStringProperty; import javafx.collections.FXCollections; import javafx.collections.ObservableLi

如何为TableView的每一行显示工具提示

例如: 当我把鼠标放在一个特定的表行上时,它会给我该行数据的详细信息


感谢您使用行工厂,并将工具提示添加到
表格行

import javafx.application.Application;
import javafx.beans.property.SimpleStringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.Insets;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableRow;
import javafx.scene.control.TableView;
import javafx.scene.control.Tooltip;
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 final TableView<Person> table = new TableView<>();
    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) {
        Scene scene = new Scene(new Group());
        stage.setTitle("Table View Sample");
        stage.setWidth(450);
        stage.setHeight(500);

        final Label label = new Label("Address Book");
        label.setFont(new Font("Arial", 20));

        table.setEditable(true);

        table.setRowFactory(tv -> new TableRow<Person>() {
            private Tooltip tooltip = new Tooltip();
            @Override
            public void updateItem(Person person, boolean empty) {
                super.updateItem(person, empty);
                if (person == null) {
                    setTooltip(null);
                } else {
                    tooltip.setText(person.getFirstName()+" "+person.getLastName());
                    setTooltip(tooltip);
                }
            }
        });

        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.setItems(data);
        table.getColumns().addAll(firstNameCol, lastNameCol, emailCol);

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

        ((Group) scene.getRoot()).getChildren().addAll(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.application.application;
导入javafx.beans.property.SimpleStringProperty;
导入javafx.collections.FXCollections;
导入javafx.collections.ObservableList;
导入javafx.geometry.Insets;
导入javafx.scene.Group;
导入javafx.scene.scene;
导入javafx.scene.control.Label;
导入javafx.scene.control.TableColumn;
导入javafx.scene.control.TableRow;
导入javafx.scene.control.TableView;
导入javafx.scene.control.Tooltip;
导入javafx.scene.control.cell.PropertyValueFactory;
导入javafx.scene.layout.VBox;
导入javafx.scene.text.Font;
导入javafx.stage.stage;
;
公共类TableViewSample扩展了应用程序{
private final TableView table=new TableView();
私有最终可观测列表数据=
FXCollections.observableArrayList(
新人(“雅各布”、“史密斯”、“雅各布”。smith@example.com"),
新人(“伊莎贝拉”、“约翰逊”、“伊莎贝拉”。johnson@example.com"),
新人(“伊桑”、“威廉姆斯”、“伊桑”。williams@example.com"),
新人(“艾玛”、“琼斯”、“艾玛”。jones@example.com"),
新人(“迈克尔”、“布朗”、“迈克尔”。brown@example.com")
);
公共静态void main(字符串[]args){
发射(args);
}
@凌驾
公众假期开始(阶段){
场景=新场景(新组());
stage.setTitle(“表格视图示例”);
舞台布景宽度(450);
舞台设置高度(500);
最终标签=新标签(“地址簿”);
label.setFont(新字体(“Arial”,20));
table.setEditable(true);
table.setRowFactory(tv->new TableRow(){
专用工具提示工具提示=新工具提示();
@凌驾
public void updateItem(Person,布尔值为空){
super.updateItem(person,空);
if(person==null){
设置工具提示(空);
}否则{
tooltip.setText(person.getFirstName()+“”+person.getLastName());
设置工具提示(工具提示);
}
}
});
TableColumn firstNameCol=新的TableColumn(“名字”);
firstNameCol.setMinWidth(100);
firstNameCol.setCellValueFactory(
新财产价值工厂(“名字”);
TableColumn lastNameCol=新的TableColumn(“姓氏”);
lastNameCol.setMinWidth(100);
lastNameCol.setCellValueFactory(
新财产价值工厂(“姓氏”);
TableColumn emailCol=新的TableColumn(“电子邮件”);
设置最小宽度(200);
emailCol.setCellValueFactory(
新物业价值工厂(“电子邮件”);
表2.设置项目(数据);
table.getColumns().addAll(firstNameCol、lastNameCol、emailCol);
最终VBox VBox=新的VBox();
vbox.setspace(5);
设置填充(新的插入(10,0,0,10));
vbox.getChildren().addAll(标签、表格);
((组)scene.getRoot()).getChildren().addAll(vbox);
舞台场景;
stage.show();
}
公共静态类人员{
私有最终SimpleStringProperty名字;
私有最终SimpleStringProperty姓氏;
私人最终SimpleStringProperty电子邮件;
私人(字符串fName、字符串lName、字符串电子邮件){
this.firstName=新的SimpleStringProperty(fName);
this.lastName=新的SimpleStringProperty(lName);
this.email=新的SimpleStringProperty(电子邮件);
}
公共字符串getFirstName(){
返回firstName.get();
}
public void setFirstName(字符串fName){
firstName.set(fName);
}
公共字符串getLastName(){
返回lastName.get();
}
public void setLastName(字符串fName){
lastName.set(fName);
}
公共字符串getEmail(){
返回email.get();
}
public void setEmail(字符串fName){
email.set(fName);
}
}
} 

您可以创建动态TableRow实现:

import java.util.function.Function;

import javafx.scene.control.TableRow;
import javafx.scene.control.Tooltip;

public class TooltipTableRow<T> extends TableRow<T> {

  private Function<T, String> toolTipStringFunction;

  public TooltipTableRow(Function<T, String> toolTipStringFunction) {
    this.toolTipStringFunction = toolTipStringFunction;
  }

  @Override
  protected void updateItem(T item, boolean empty) {
    super.updateItem(item, empty);
    if(item == null) {
      setTooltip(null);
    } else {
      Tooltip tooltip = new Tooltip(toolTipStringFunction.apply(item));
      setTooltip(tooltip);
    }
  }
}
personTableView.setRowFactory((tableView) -> {
      return new TooltipTableRow<Person>((Person person) -> {
        return person.getFirstName()+" "+person.getLastName();
      });
});