JavaFx ListView 在每个项目中设置工具提示

在下面尝试我的代码并重新打开窗口时,ListView 工具提示未显示。

Tooltip tooltip1 = new Tooltip(timeToday());
listView.setTooltip(tooltip1);


白衣染霜花
浏览 80回答 1
1回答

呼如林

这里可能已经有几个答案解释了如何ListCell在 a 中CellFactory为 the提供您自己的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 {&nbsp; &nbsp; public static void main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp; launch(args);&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public void start(Stage primaryStage) {&nbsp; &nbsp; &nbsp; &nbsp; // Simple Interface&nbsp; &nbsp; &nbsp; &nbsp; VBox root = new VBox(10);&nbsp; &nbsp; &nbsp; &nbsp; root.setAlignment(Pos.CENTER);&nbsp; &nbsp; &nbsp; &nbsp; root.setPadding(new Insets(10));&nbsp; &nbsp; &nbsp; &nbsp; // Create a simple ListView&nbsp; &nbsp; &nbsp; &nbsp; ListView<Book> lvBooks = new ListView<>();&nbsp; &nbsp; &nbsp; &nbsp; // Populate our ListView with some sample books&nbsp; &nbsp; &nbsp; &nbsp; lvBooks.getItems().addAll(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new Book("Great Expectations", "Charles Dickens"),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new Book("Pride and Prejudice", "Jane Austen"),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new Book("To Kill a Mockingbird", "Harper Lee"),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new Book("1984", "George Orwell")&nbsp; &nbsp; &nbsp; &nbsp; );&nbsp; &nbsp; &nbsp; &nbsp; // We want to display the title of the Book in our ListView and show the author as a Tooltip&nbsp; &nbsp; &nbsp; &nbsp; // To do so, let's provide our own CellFactory&nbsp; &nbsp; &nbsp; &nbsp; lvBooks.setCellFactory(cell -> new ListCell<Book>() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // We want to create a single Tooltip that will be reused, as needed. We will simply update the text&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // for the Tooltip for each cell&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; final Tooltip tooltip = new Tooltip();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // We will override the updateItem() method of the ListCell so we can setup exactly what we want displayed&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // in each cell.&nbsp; For this example, we'll just use the text of the cell, but you could also provide your&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // own layout entirely, by calling setGraphic() instead of the setText() used below.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; protected void updateItem(Book book, boolean empty) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; super.updateItem(book, empty);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (book == null || empty) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // No book to display here (empty cell) so we clear the text and Tooltip&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; setText(null);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; setTooltip(null);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // A book is to be listed in this cell&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; setText(book.getTitle());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Let's show our Author when the user hovers the mouse cursor over this row&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tooltip.setText(book.getAuthor());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; setTooltip(tooltip);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; // Finally, add the ListView to our root VBox&nbsp; &nbsp; &nbsp; &nbsp; root.getChildren().add(lvBooks);&nbsp; &nbsp; &nbsp; &nbsp; // Show the stage&nbsp; &nbsp; &nbsp; &nbsp; primaryStage.setScene(new Scene(root));&nbsp; &nbsp; &nbsp; &nbsp; primaryStage.setTitle("CustomListCellSample Sample");&nbsp; &nbsp; &nbsp; &nbsp; primaryStage.show();&nbsp; &nbsp; }}// Simple Book class to be used as our data modelclass Book {&nbsp; &nbsp; private final StringProperty title = new SimpleStringProperty();&nbsp; &nbsp; private final StringProperty author = new SimpleStringProperty();&nbsp; &nbsp; public Book(String title, String author) {&nbsp; &nbsp; &nbsp; &nbsp; this.title.set(title);&nbsp; &nbsp; &nbsp; &nbsp; this.author.set(author);&nbsp; &nbsp; }&nbsp; &nbsp; public String getTitle() {&nbsp; &nbsp; &nbsp; &nbsp; return title.get();&nbsp; &nbsp; }&nbsp; &nbsp; public StringProperty titleProperty() {&nbsp; &nbsp; &nbsp; &nbsp; return title;&nbsp; &nbsp; }&nbsp; &nbsp; public void setTitle(String title) {&nbsp; &nbsp; &nbsp; &nbsp; this.title.set(title);&nbsp; &nbsp; }&nbsp; &nbsp; public String getAuthor() {&nbsp; &nbsp; &nbsp; &nbsp; return author.get();&nbsp; &nbsp; }&nbsp; &nbsp; public StringProperty authorProperty() {&nbsp; &nbsp; &nbsp; &nbsp; return author;&nbsp; &nbsp; }&nbsp; &nbsp; public void setAuthor(String author) {&nbsp; &nbsp; &nbsp; &nbsp; this.author.set(author);&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java