- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有书和作者:
public Author(String name, Date dateOfBirth) {
this.name=name;
this.dateOfBirth = dateOfBirth;
}
<小时/>
public Book(String isbn, String title, String category, int rating, Author author) {
this.isbn = isbn;
this.title = title;
this.category = category;
this.rating = rating;
authors = new ArrayList<>();
addAuthor(author);
}
除了作者之外,一切都运行良好并显示在表格中。我也想在表中显示作者姓名,但当我运行程序时,它的列是空的。同一张表中可以有两个对象吗?如何让作者栏也显示姓名? (PS:书有作者)
<小时/> booksTable = new TableView<>();
booksTable.setEditable(true);
TableColumn<Book, String> titleCol = new TableColumn<>("Title");
TableColumn<Book, String> isbnCol = new TableColumn<>("ISBN");
TableColumn<Book, String> categoryCol = new TableColumn<>("Category");
TableColumn<Book, Author> authorCol = new TableColumn<>("Author/s");
TableColumn<Book, String> ratingCol = new TableColumn<>("Rating");
booksTable.getColumns().addAll(titleCol, isbnCol, categoryCol,authorCol,ratingCol);
titleCol.setCellValueFactory(new PropertyValueFactory<>("title"));
isbnCol.setCellValueFactory(new PropertyValueFactory<>("isbn"));
categoryCol.setCellValueFactory(new PropertyValueFactory<>("category"));
authorCol.setCellValueFactory(new PropertyValueFactory<>("author"));
ratingCol.setCellValueFactory(new PropertyValueFactory<>("rating"));
booksTable.setItems(booksInTable);
最佳答案
您的Author
类似乎没有提供任何将作者姓名传递给TableColumn
的方法。
一种方法是重写 Author
类中的 toString()
方法,如下面的示例应用程序所示。
虽然这确实使用了实例字段的 JavaFX 属性,但概念是相同的:
import javafx.application.Application;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleObjectProperty;
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.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class LibrarySample extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
// Simple interface
VBox root = new VBox(5);
root.setPadding(new Insets(10));
root.setAlignment(Pos.CENTER);
TableView<Book> tableView = new TableView<>();
TableColumn<Book, String> colTitle = new TableColumn<>("Title");
TableColumn<Book, Author> colAuthor = new TableColumn<>("Author");
colTitle.setCellValueFactory(cellData -> cellData.getValue().titleProperty());
colAuthor.setCellValueFactory(cellData -> cellData.getValue().authorProperty());
tableView.getColumns().addAll(colTitle, colAuthor);
// Sample books
tableView.getItems().addAll(
new Book("To Kill A Mockingbird", new Author("Harper Lee")),
new Book("David Copperfield", new Author("Charles Dickens")),
new Book("Moby Dick", new Author("Herman Melville"))
);
root.getChildren().add(tableView);
// Show the Stage
primaryStage.setWidth(500);
primaryStage.setHeight(300);
primaryStage.setScene(new Scene(root));
primaryStage.show();
}
}
class Book {
private final StringProperty title = new SimpleStringProperty();
private final ObjectProperty<Author> author = new SimpleObjectProperty<>();
public Book(String title, Author 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 Author getAuthor() {
return author.get();
}
public ObjectProperty<Author> authorProperty() {
return author;
}
public void setAuthor(Author author) {
this.author.set(author);
}
}
class Author {
private final StringProperty name = new SimpleStringProperty();
public Author(String name) {
this.name.set(name);
}
public String getName() {
return name.get();
}
public StringProperty nameProperty() {
return name;
}
public void setName(String name) {
this.name.set(name);
}
// Here we will override the toString() method so that the author's name is displayed in the TableView
@Override
public String toString() {
return name.get();
}
}
<小时/>
Result:
关于Java:TableColumn 用于 2 个对象 Book 和 Author,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53624326/
我正在为 MIT OCW 类(class)做一些事情,它要求编写一个“图书馆”类(class)。现在我有这个: #include using namespace std; class Book{
ExcelWorkbook = py.load_workbook(FilePath) writer = pd.ExcelWriter(FilePath, engine = 'openpyxl') wr
我是 C++ 的学习者,我对构造函数和析构函数感兴趣。我编译了下面的代码,它返回了对 Book::~Book() 错误的 undefined reference 。但是当我注释掉析构函数时,它工作正常
按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我正在使用 Google Books API在我的 Angular 项目中。我有书籍的不同静态类别的列表。单击特定类别时,我想从 Google Books API 获取搜索类别的书籍。 Google
我不知道如何做一些应该非常简单的事情。 我有两个实体:书架和书。一个书架可以放一本或多本书。这些实体中的每一个都有一个相应的 JpaRepository 暴露为使用 Spring Data Rest
我需要从 Booking.com 系统获取特定住宿的评分值。是否有 API 可以提供总体评分等信息以及系统中列出的属性的其他有用信息? 最佳答案 如果有人有同样的问题,我会简短地回答 - bookin
我需要从 Booking.com 系统获取特定住宿的评分值。是否有 API 可以提供总体评分等信息以及系统中列出的属性的其他有用信息? 最佳答案 如果有人有同样的问题,我会简短地回答 - bookin
有没有一种简单的方法可以使用 Google Book API 从 ISBN 获取 JSON 格式的图书封面? 最佳答案 您可以使用 isbn: 查询,如下所示: https://www.googlea
我是新手,所以这只是一个问题,我想知道哪个更有效,哪个提供最佳时间复杂度。 没有。 1 export default class BookingTabs extends Component {
在我一直在开发的应用程序中,我遇到了像 /books/:slug, :to => 'books#show', slug:/.*?/ 这样的路由。我很好奇这部分的作用 slug:/.*?/ ? 最佳答案
刚从使用 Books 应用程序示例的 Djangobook 教程中学习时,您通过多对多关系将 Book 与 Author 相关,并将 Book 与 Publisher 相关。您可以使用 p.book_
我刚启动 xcode 7 稳定版。在我当前的项目中,我正在从 web 服务下载图像。在 xcode 6.4 中工作正常。现在它没有显示任何图像并在日志中显示警告 -canOpenURL: failed
我在名为 DetailOrder 和 Book 的两个类中遇到映射问题。 问题如下所示。 Initial SessionFactory creation failed. org.hibernate.A
我正在尝试制作 CRUD+spring 应用程序来创建/删除/更新书籍。一切正常,但从数据库中搜索一本书。请帮忙。 @Controller public class BookController {
我完全是使用 CakePhp 的新手。我已经解决了一些问题,但我又对这个基本问题了如指掌。你能帮我解决这个问题吗? Notice (8): Undefined variable: books [APP
我在网上冲浪时遇到了一对多关系的问题,并且无法修复。我仔细检查了他们引用的 @Entity 声明 import javax.persistence.Entity; 之前我仅尝试单向 ManyToOne
刚开始在我的学校学习 node js。他们给了我们这个半完成的任务,我需要让下一个和上一个按钮起作用。但是,当我运行 index.html 时,控制台出现了一些错误。错误是: “获取 API 无法加载
我创建了一个网站,访问者可以使用turnjs 浏览一本书。我的页面是双页的 jpg,我希望它们能够动态加载。 代码如下: var flipbook = $('.flipbook'); flipboo
void displayInventory(const struct Book book[], const int size) { Idk y book[] 在 visual studio 中遇到
我是一名优秀的程序员,十分优秀!