gpt4 book ai didi

Java:TableColumn 用于 2 个对象 Book 和 Author

转载 作者:行者123 更新时间:2023-12-02 01:46:29 26 4
gpt4 key购买 nike

我有书和作者:

 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:

screenshot

关于Java:TableColumn 用于 2 个对象 Book 和 Author,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53624326/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com