java - JavaFx ListView 在每个项目中设置工具提示
问题描述
当在下面尝试我的代码并重新打开窗口时,没有显示 ListView 工具提示。
Tooltip tooltip1 = new Tooltip(timeToday());
listView.setTooltip(tooltip1);
解决方案
这里可能已经有几个答案解释了如何ListCell
在 aCellFactory
中为提供您自己的答案ListView
,但下面是一个完整的示例,详细说明了该问题以及Tooltip
在每个条目上设置 a :
import javafx.application.Application;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.ListCell;
import javafx.scene.control.ListView;
import javafx.scene.control.Tooltip;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class CustomListCellSample extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
// Simple Interface
VBox root = new VBox(10);
root.setAlignment(Pos.CENTER);
root.setPadding(new Insets(10));
// Create a simple ListView
ListView<Book> lvBooks = new ListView<>();
// Populate our ListView with some sample books
lvBooks.getItems().addAll(
new Book("Great Expectations", "Charles Dickens"),
new Book("Pride and Prejudice", "Jane Austen"),
new Book("To Kill a Mockingbird", "Harper Lee"),
new Book("1984", "George Orwell")
);
// We want to display the title of the Book in our ListView and show the author as a Tooltip
// To do so, let's provide our own CellFactory
lvBooks.setCellFactory(cell -> new ListCell<Book>() {
// We want to create a single Tooltip that will be reused, as needed. We will simply update the text
// for the Tooltip for each cell
final Tooltip tooltip = new Tooltip();
// We will override the updateItem() method of the ListCell so we can setup exactly what we want displayed
// in each cell. For this example, we'll just use the text of the cell, but you could also provide your
// own layout entirely, by calling setGraphic() instead of the setText() used below.
@Override
protected void updateItem(Book book, boolean empty) {
super.updateItem(book, empty);
if (book == null || empty) {
// No book to display here (empty cell) so we clear the text and Tooltip
setText(null);
setTooltip(null);
} else {
// A book is to be listed in this cell
setText(book.getTitle());
// Let's show our Author when the user hovers the mouse cursor over this row
tooltip.setText(book.getAuthor());
setTooltip(tooltip);
}
}
});
// Finally, add the ListView to our root VBox
root.getChildren().add(lvBooks);
// Show the stage
primaryStage.setScene(new Scene(root));
primaryStage.setTitle("CustomListCellSample Sample");
primaryStage.show();
}
}
// Simple Book class to be used as our data model
class Book {
private final StringProperty title = new SimpleStringProperty();
private final StringProperty author = new SimpleStringProperty();
public Book(String title, String author) {
this.title.set(title);
this.author.set(author);
}
public String getTitle() {
return title.get();
}
public StringProperty titleProperty() {
return title;
}
public void setTitle(String title) {
this.title.set(title);
}
public String getAuthor() {
return author.get();
}
public StringProperty authorProperty() {
return author;
}
public void setAuthor(String author) {
this.author.set(author);
}
}
推荐阅读
- java - elasticSearch 动态术语字段
- telerik - 如何解决 Telerik Rad 按钮的滚动问题
- python - PySpark 加入多列
- reactjs - 使用 RTK 查询的现代 Redux React,表单未添加到服务器
- mongodb - 用于创建列出所有集合和集合字段的可嵌入表的 MongoDB 图表
- node.js - 为什么在我的 node.js 应用程序中创建新评论不起作用?
- amazon-web-services - 为什么我收到错误使用命令 amazon cli?
- python - 使用带有标题样式的 Python 将字典转换为 CSV
- reactjs - 使用钩子由箭头键控制的自定义组合框
- typescript - Vue 配置文件。编译一次,根据 deploymnet 级别更改文件