【问题标题】:My Object isnt getting printed to my JavaFX Table View我的对象没有打印到我的 JavaFX 表视图
【发布时间】:2019-08-02 12:40:49
【问题描述】:

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

我正在尝试使用我的控制器中的 JavaFX 将这些打印到表格视图中,但即使放置了 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);
        }
    });
   }
}


【问题讨论】:

  • 这些属性似乎没有吸气剂。 PropertyValueFactory 依赖于检索这些列值的方法。至于“十六进制代码”:stackoverflow.com/questions/29140402/…
  • 嘿,既然我已经从我的外部类中输入了对象,在这种情况下它是否一定需要一个吸气剂?
  • 你的MusicItem 类没有Property...所以不仅没有getter,而且甚至没有要查找的属性。
  • @Ryan 查看the documentation of PropertyValueFactory。它使用反射来定位适当的 property-getter 方法,或者,如果该方法不存在,则定位适当的 getter 方法。它还采用标准的 Java Bean 命名约定。因此,如果您有一个属性x,那么您需要一个getX() 方法、一个setX(X) 方法(如果可写)和一个xProperty() 方法(如果它是JavaFX 属性)。
  • @Slaw 这是否仍然需要模型类中存在属性?我只看到String 和其他原语...

标签: java mongodb javafx tableview morphia


【解决方案1】:

想通了。我写的代码从来没有错。我调用的 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;
}

【讨论】:

  • hmm ...如果可行(我很高兴您找到了解决方案:),您没有在问题中显示相关代码:特别是数据对象(MusicItem 和后代) 是不完整的——没有 getter/setter,PropertyValueFactory 什么也不做。所以下次,请提供一个minimal reproducible example 来说明问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多