我想将数据库中的一些数据加载到我的tableview中。我有一个代码,它工作正常,但没有我的tableview是空的.... 这是我的fxml文件:
<TableView fx:id="libraryNode" editable="true" onKeyPressed="#onLibraryRequest" xmlns:fx="http://javafx.com/fxml" fx:controller="mediabox.app.controller.MusicscreenController">
<columns>
<TableColumn text="Index" prefWidth="40" >
<cellValueFactory>
<PropertyValueFactory property="id" />
</cellValueFactory>
</TableColumn>
<TableColumn text="Titel" prefWidth="150">
<cellValueFactory>
<PropertyValueFactory property="titel" />
</cellValueFactory>
</TableColumn>
<TableColumn text="Dauer" prefWidth="150" >
<cellValueFactory>
<PropertyValueFactory property="playtime" />
</cellValueFactory>
</TableColumn>
<TableColumn text="Interpret" prefWidth="150">
<cellValueFactory>
<PropertyValueFactory property="interpret" />
</cellValueFactory>
</TableColumn>
<TableColumn text="Album" prefWidth="150">
<cellValueFactory>
<PropertyValueFactory property="album" />
</cellValueFactory>
</TableColumn>
<TableColumn text="Genre" prefWidth="150" >
<cellValueFactory>
<PropertyValueFactory property="genre" />
</cellValueFactory>
</TableColumn>
<TableColumn text="Bewertung" prefWidth="60">
<cellValueFactory>
<PropertyValueFactory property="score" />
</cellValueFactory>
</TableColumn>
</columns>
这是我的超级类别的mediatypes:
public abstract class Medium {
private static int id;
private String filepath;
private final Media mediaResource;
private final SimpleStringProperty titel = new SimpleStringProperty();
private final SimpleStringProperty genre = new SimpleStringProperty();
private final SimpleDoubleProperty score = new SimpleDoubleProperty();
protected Medium(String filepath, String titel, String genre, double score) {
setId(id++);
setFilepath(filepath);
setTitel(titel);
setGenre(genre);
setScore(score);
this.mediaResource = new Media(new File(getFilepath()).toURI().toString());
}
public static int getId() {
return id;
}
public static void setId(int id) {
Medium.id = id;
}
public final StringProperty titelProperty() {
return this.titel;
}
public String getTitel() {
return titelProperty().get();
}
public final void setTitel(String titel) {
titelProperty().set(titel);
}
public final StringProperty genreProperty() {
return this.genre;
}
public String getGenre() {
return genreProperty().get();
}
public final void setGenre(String genre) {
genreProperty().set(genre);
}
public final DoubleProperty scoreProperty() {
return this.score;
}
public double getScore() {
return scoreProperty().get();
}
public final void setScore(double score) {
scoreProperty().set(score);
}
public final String getFilepath() {
return this.filepath;
}
public final void setFilepath(String filepath) {
this.filepath = filepath;
}
public Media getMediaResource() {
return mediaResource;
}
}
这是音乐课:
public final class Music extends Medium {
private final SimpleStringProperty playtime = new SimpleStringProperty();
private final SimpleStringProperty interpret = new SimpleStringProperty();
private final SimpleStringProperty album = new SimpleStringProperty();
private final SimpleStringProperty genre = new SimpleStringProperty();
private final SimpleDoubleProperty score = new SimpleDoubleProperty();
/**
*
* @param titel
* @param playtime
* @param interpret
* @param album
* @param genre
* @param score
* @param filepath
*/
public Music(String titel,String playtime, String interpret,
String album, String genre, double score, String filepath) {
super(filepath, titel,genre, score);
setPlaytime(playtime);
setInterpret(interpret);
setAlbum(album);
}
public final StringProperty playtimeProperty() {
return this.playtime;
}
public String getPlaytime() {
return playtimeProperty().get();
}
public void setPlaytime(String playtime) {
playtimeProperty().set(playtime);
}
public final StringProperty interpretProperty() {
return this.interpret;
}
public String getInterpret() {
return interpretProperty().get();
}
public void setInterpret(String interpret) {
interpretProperty().set(interpret);
}
public final StringProperty albumProperty() {
return this.album;
}
public String getAlbum() {
return albumProperty().get();
}
public void setAlbum(String album) {
albumProperty().set(album);
}
}
这是将数据库中的数据加载到tableview中的控制器:
public final class MusicscreenController extends Controller implements Initializable {
@FXML
public TableView libraryNode;
@FXML
private MediaView mediaPlayerView;
/**
* Initialisiert die Bibliothek
*/
@Override
protected void initLibrary() {
try {
DatabaseConnector.connectTo("src/mediabox/database/database");
boolean setAll = libraryNode.getItems().addAll(DatabaseConnector.loadEntries("Music")); // Einträge der Datenbank
// auslesen und der library Node hinzufügen
} catch (SQLException | ConnectionException | NamingException | ClassNotFoundException ex) {
Logger.getLogger(MusicscreenController.class.getName()).log(Level.SEVERE, null, ex);
}
}
@Override
public void initialize(URL url, ResourceBundle rb) {
libraryNode = new TableView<Music>();
initLibrary();
libraryNode.requestFocus();
libraryNode.getSelectionModel().selectFirst();
}
@FXML
public void onLibraryRequest(KeyEvent e) {
if (e.getCode().equals(KeyCode.ENTER)) {
try {
MediaPlayerController mediaPlayerController = new MediaPlayerController((Music) libraryNode.getSelectionModel().getSelectedItem());
} catch (IOException ex) {
Logger.getLogger(MusicscreenController.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
没有错误消息或类似的内容。 TableView仅显示TableView&#34;中没有内容。 DatabaseConnector类工作正常。我测试了加载的ArrayList是否包含正确的数据。我无法在此代码中找到错误... AarrayList包含所有音乐对象,但表格并不代表它们。所以我认为我在tableview的设计中遇到了问题。
-GhostfaceChilla -
答案 0 :(得分:1)
在initialize方法中,您不应该使用
libraryNode = new TableView<Music>();
标记为@FXML
的Controller的所有组件在加载FXML时被初始化