我的对象没有打印到我的 JavaFX Table 视图
My Object isnt getting printed to my JavaFX Table View
我目前通过一个构造函数传递我的数据,该构造函数填充从我的抽象和扩展 类 请求的数据(即 MusicItem Abstract 和 CD 和 Vinyl 类 扩展)
我正在尝试使用控制器中的 JavaFX 将这些打印到 Table 视图中,但即使放置了 system.out 行也没有显示任何内容。
我试过将它们放入 Observable 列表和普通数组列表中,我已经三次检查数据字段以查看它们是否与 table 匹配,我现在尝试查看对象它输入了但是当我这样做时,该对象的十六进制代码出现了。
它正确连接到数据库但不显示任何内容
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 连接这些值,并通过将它们添加到可观察列表中来获取这些值。并将 table 列和数据设置为任何结果并且效果很好
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;
}
我目前通过一个构造函数传递我的数据,该构造函数填充从我的抽象和扩展 类 请求的数据(即 MusicItem Abstract 和 CD 和 Vinyl 类 扩展)
我正在尝试使用控制器中的 JavaFX 将这些打印到 Table 视图中,但即使放置了 system.out 行也没有显示任何内容。
我试过将它们放入 Observable 列表和普通数组列表中,我已经三次检查数据字段以查看它们是否与 table 匹配,我现在尝试查看对象它输入了但是当我这样做时,该对象的十六进制代码出现了。 它正确连接到数据库但不显示任何内容
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 连接这些值,并通过将它们添加到可观察列表中来获取这些值。并将 table 列和数据设置为任何结果并且效果很好
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;
}