我有书和作者:
os.environ['APPDATA']
public Author(String name, Date dateOfBirth) {
this.name=name;
this.dateOfBirth = dateOfBirth;
}
一切正常,但作者一切正常,并显示在表格中。我也想在表中显示作者姓名,但是当我运行程序时,该列的内容为空。我可以在同一个表中有两个对象吗?如何使“作者”列也显示名称? (PS:书中有作者)
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);
}
答案 0 :(得分:2)
您的Author
类似乎没有提供任何将作者姓名传递给TableColumn
的方法。
一种方法是覆盖toString()
类中的Author
方法,如下面的示例应用程序所示。
尽管确实为实例字段使用了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();
}
}
结果: