我迷路了。实际上,有一个javafx tableview包含几行。只有一列。使用Tooltip类是为了将行文本内容显示为气泡。当鼠标光标悬停在该行上方时,会显示该气泡;当鼠标光标离开该行时,气泡必须消失。 问题在于,有时气泡不会消失。它停留在屏幕上,并且不再移动。但是,可以移动和使用该应用程序。气泡不会移动,并且似乎停留在计算机背景上。 您是否知道如何解决这个问题?
下面的代码用于创建和关闭气泡:
public void bindTooltip(final Node node, final Tooltip tooltip) {
node.setOnMouseMoved(event -> tooltip.show(node, event.getScreenX(), event.getScreenY() + 15));
node.setOnMouseExited((event) -> hideTooltip(tooltip));
}
private void hideTooltip(Tooltip tooltip) {
if (tooltip.isShowing()) {
tooltip.hide();
}
}
bindTooltip方法用于将行绑定到工具提示。
谢谢您的帮助。
习惯上
Vinz
答案 0 :(得分:0)
尝试使用table.setRowFactory
设置ToolTip
。下面的例子。来自here的代码。
键码!
table.setRowFactory(tv -> {
TableRow<UserAccount> row = new TableRow();
Tooltip tooltip = new Tooltip();
row.setOnMouseEntered((event) -> {
if (!row.isEmpty()) {
UserAccount tempUserAccount = row.getItem();
tooltip.setText(tempUserAccount.getLastName() + ", " + tempUserAccount.getFirstName());
row.setTooltip(tooltip);
}
});
return row;
});
完整代码
主要
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.Insets;
import javafx.scene.Scene;
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.StackPane;
import javafx.stage.Stage;
public class TableViewDemo2 extends Application
{
@Override
public void start(Stage stage)
{
TableView<UserAccount> table = new TableView<UserAccount>();
// Create column UserName (Data type of String).
TableColumn<UserAccount, String> userNameCol //
= new TableColumn<UserAccount, String>("User Name");
// Create column Email (Data type of String).
TableColumn<UserAccount, String> emailCol//
= new TableColumn<UserAccount, String>("Email");
// Create column FullName (Data type of String).
TableColumn<UserAccount, String> fullNameCol//
= new TableColumn<UserAccount, String>("Full Name");
// Create 2 sub column for FullName.
TableColumn<UserAccount, String> firstNameCol//
= new TableColumn<UserAccount, String>("First Name");
TableColumn<UserAccount, String> lastNameCol //
= new TableColumn<UserAccount, String>("Last Name");
// Add sub columns to the FullName
fullNameCol.getColumns().addAll(firstNameCol, lastNameCol);
// Active Column
TableColumn<UserAccount, Boolean> activeCol//
= new TableColumn<UserAccount, Boolean>("Active");
// Defines how to fill data for each cell.
// Get value from property of UserAccount. .
userNameCol.setCellValueFactory(new PropertyValueFactory<>("userName"));
emailCol.setCellValueFactory(new PropertyValueFactory<>("email"));
firstNameCol.setCellValueFactory(new PropertyValueFactory<>("firstName"));
lastNameCol.setCellValueFactory(new PropertyValueFactory<>("lastName"));
activeCol.setCellValueFactory(new PropertyValueFactory<>("active"));
// Set Sort type for userName column
userNameCol.setSortType(TableColumn.SortType.DESCENDING);
lastNameCol.setSortable(false);
// Display row data
ObservableList<UserAccount> list = getUserList();
table.setItems(list);
table.getColumns().addAll(userNameCol, emailCol, fullNameCol, activeCol);
table.setRowFactory(tv -> {
TableRow<UserAccount> row = new TableRow();
Tooltip tooltip = new Tooltip();
row.setOnMouseEntered((event) -> {
if (!row.isEmpty()) {
UserAccount tempUserAccount = row.getItem();
tooltip.setText(tempUserAccount.getLastName() + ", " + tempUserAccount.getFirstName());
row.setTooltip(tooltip);
}
});
return row;
});
StackPane root = new StackPane();
root.setPadding(new Insets(5));
root.getChildren().add(table);
stage.setTitle("TableView (o7planning.org)");
Scene scene = new Scene(root, 450, 300);
stage.setScene(scene);
stage.show();
}
private ObservableList<UserAccount> getUserList()
{
UserAccount user1 = new UserAccount(1L, "smith", "smith@gmail.com", //
"Susan", "Smith", true);
UserAccount user2 = new UserAccount(2L, "mcneil", "mcneil@gmail.com", //
"Anne", "McNeil", true);
UserAccount user3 = new UserAccount(3L, "white", "white@gmail.com", //
"Kenvin", "White", false);
ObservableList<UserAccount> list = FXCollections.observableArrayList(user1, user2, user3);
return list;
}
public static void main(String[] args)
{
launch(args);
}
}
UserAccount
public class UserAccount
{
private Long id;
private String userName;
private String email;
private String firstName;
private String lastName;
private boolean active;
public UserAccount(Long id, String userName, String email, //
String firstName, String lastName, boolean active)
{
this.id = id;
this.userName = userName;
this.email = email;
this.firstName = firstName;
this.lastName = lastName;
this.active = active;
}
public Long getId()
{
return id;
}
public void setId(Long id)
{
this.id = id;
}
public String getUserName()
{
return userName;
}
public void setUserName(String userName)
{
this.userName = userName;
}
public String getEmail()
{
return email;
}
public void setEmail(String email)
{
this.email = email;
}
public String getFirstName()
{
return firstName;
}
public void setFirstName(String firstName)
{
this.firstName = firstName;
}
public String getLastName()
{
return lastName;
}
public void setLastName(String lastName)
{
this.lastName = lastName;
}
public boolean isActive()
{
return active;
}
public void setActive(boolean active)
{
this.active = active;
}
}