java - 我的对象未打印到我的 JavaFX TableView

标签 java mongodb javafx tableview morphia

我目前正在通过一个构造函数传递我的数据,该构造函数填充我的抽象类和扩展类(即 MusicItem Abstract 和 CD 和 Vinyl 类扩展)请求的数据

我尝试使用 Controller 中的 JavaFX 将这些内容打印到 TableView 中,但即使放置了 system.out 行,也没有显示任何内容。

我尝试将它们放入可观察列表以及普通数组列表中,我已经三次检查数据字段以查看它们是否与表匹配,并且我尝试在输入对象时查看对象,但是当我这样做时,会显示该对象的十六进制代码。 它正确连接到数据库但不显示任何内容

abstract class MusicItem {
    @Id
    private int songID;
    private String name;
    private String artist;
    private String genre;
    private String releaseDate;
    private double price;

public MusicItem(int songID, String name, String artist, String genre, String releaseDate, double price) {
    this.songID = songID;
    this.name = name;
    this.artist = artist;
    this.genre = genre;
    this.releaseDate = releaseDate;
    this.price = price;
    }
}


@Entity
class Vinyl extends MusicItem{

private double speed;
private double diameter;

Vinyl(int songID, String name, String artist, String genre, String releaseDate, double price, double speed, double diameter) {
    super(songID, name, artist, genre, releaseDate, price);
    this.speed = speed;
    this.diameter = diameter;
    }
}


@Entity
class CD extends MusicItem{

private double duration;

CD(int songID, String name, String artist, String genre, String releaseDate, double price, double duration) {
    super(songID, name, artist, genre, releaseDate, price);
    this.duration = duration;
   }
}


public WestminsterMusicStoreManager() throws UnknownHostException {
}

@Override
public void insertItem() {
    System.out.print("Please enter Song ID : ");
    id = Validation.intValidate();
    System.out.print("Please enter Song name : ");
    name = Validation.stringValidate();
    System.out.print("Please enter Artist : ");
    artist = Validation.stringValidate();
    System.out.print("Please enter genre of  " + name + " : ");
    genre = Validation.stringValidate();
    System.out.println("Please enter the release date in the requested order: ");
    releaseDate = date.getDate();
    System.out.print("Please enter price of song : ");
    price = Validation.doubleValidate();

    System.out.println("Will you be entering a CD or Vinyl Entry");
    String choice = Validation.stringValidate();
    switch (choice.toLowerCase()){
        case "vinyl":
            System.out.print("Please enter diameter of vinyl : ");
            diameter = Validation.doubleValidate();
            System.out.print("Please enter speed of vinyl : ");
            speed = Validation.doubleValidate();
            Vinyl vinyl = new Vinyl(id,name,artist,genre,releaseDate,price,speed,diameter);
            musicItemList.add(vinyl);
            database.insertVinyl(vinyl);
            System.out.println(name+" was succesfully added to the database with an ID of"+id);
            break;


        case "cd":
            System.out.println("Please enter duration of the song");
            duration = Validation.doubleValidate();
            CD cd = new CD(id,name,artist,genre,releaseDate,price,duration);
            musicItemList.add(cd);
            System.out.println(cd);
            database.insertCD(cd);
            break;

            default:
                System.out.println("Your value needs to be a choice between either CD or Vinyl");
                insertItem();
    }
}
}

public class Controller implements Initializable {
public GridPane customerMainLayout;
@FXML private TableColumn<MusicItem, String> artistCol, songNameCol, durationCol, genreCol;
@FXML private TableColumn<MusicItem, String> priceCol;
@FXML private TableColumn<MusicItem, Double> speedCol,diameterCol;
@FXML private TableColumn<MusicItem, Integer> songIDCol, releaseYearCol;
public TableView<MusicItem> customerViewTable;
@FXML private static JFXTextField searchBar;
@FXML private static JFXButton searchBtn;

private WestminsterMusicStoreManager musicStoreManager =new WestminsterMusicStoreManager();
private ObservableList<MusicItem> musicItem = FXCollections.observableArrayList(musicStoreManager.musicItemList);

public Controller() throws UnknownHostException {
}

public void initialize(URL location, ResourceBundle resources) {

    songIDCol.setCellValueFactory(new PropertyValueFactory<>("songID"));
    songNameCol.setCellValueFactory(new PropertyValueFactory<>("name"));
    artistCol.setCellValueFactory(new PropertyValueFactory<>("artist"));
    genreCol.setCellValueFactory(new PropertyValueFactory<>("genre"));
    releaseYearCol.setCellValueFactory(new PropertyValueFactory<>("releaseDate"));
    priceCol.setCellValueFactory(new PropertyValueFactory<>("price"));
    durationCol.setCellValueFactory(new PropertyValueFactory<>("duration"));
    speedCol.setCellValueFactory(new PropertyValueFactory<>("speed"));
    diameterCol.setCellValueFactory(new PropertyValueFactory<>("diameter"));
    addTableItems();
}


private void addTableItems() {
    musicItem.forEach(musicItem -> {
        if (musicItem instanceof CD){
            CD cd = (CD)musicItem;
            System.out.println(cd);
            customerViewTable.getItems().add(cd);
        }else {
            Vinyl vinyl = (Vinyl)musicItem;
            System.out.println(vinyl);
            customerViewTable.getItems().add(vinyl);
        }
    });
   }
}


最佳答案

想通了。我写的代码从来没有错误过。我调用的 Observable 列表正在调用一个新实例,这就是为什么没有返回任何值的原因(因为实例调用后列表为空)。我直接从 MongoDB 连接这些值,并通过将它们添加到可观察列表来获取这些值。并将表列和数据设置为任何结果,效果很好

 private ObservableList<MusicItem> addTableItems() throws UnknownHostException {
    ObservableList<MusicItem> musicItem = FXCollections.observableArrayList();
    Database database = new Database();
    for (MusicItem item: database.datastore.find(MusicItem.class).asList()){
        musicItem.addAll(item);
    }
    return musicItem;
}

Result After Database inclusion

关于java - 我的对象未打印到我的 JavaFX TableView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57326697/

相关文章:

java - : "Building" 期间发生内部错误

java - MQTT Android 未连接到 ActiveMq

java - 如何在java中对复杂集合mongodb中的列表进行排序和获取?

mongodb - 如何在使用 go 从 mongodb 获取数据时对数据进行分组?

Java - 将元素添加到列表中时出错

java - 为什么 ReadOnlyDoubleProperty 不是 Double?

java - 当我们可以在 java 中覆盖 writeObject 和 readObject 时,为什么我们有 Externalizable

java - 如何在线程中注入(inject)Spring Bean

mongodb - mongo查询多个条件

Command+T 的 JavaFX 组合键(新标签)