TableView中没有内容 [英] No content in TableView

查看:102
本文介绍了TableView中没有内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将数据库中的一些数据加载到我的tableview中。我有一个代码,它工作正常但没有我的tableview是空的....
这是我的fxml文件:

I want to load some data from a database into my tableview.I've got a code and it works fine but no my tableview is empty .... Here is my fxml file:

<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 :

This my Superclass for alle 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中:

And this is the controller which loads the data from the database into the 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中没有内容
DatabaseConnector类工作正常。我测试了加载的ArrayList是否包含正确的数据。我在这段代码中找不到错误... AarrayList包含所有音乐对象,但表格并不代表它们。所以我认为我在tableview的设计中遇到了问题。

There is no error message or something like that. The TableView only shows "No content in TableView" The DatabaseConnector class works fine. I tested if the loaded ArrayList contains the right data. I can't find an error in this code ... The AarrayList contains all the music objects, but the table don't represent them. So I think I got a problem in my design of the tableview.

-GhostfaceChilla -

-GhostfaceChilla-

推荐答案

在你的initialize方法中,你是不应该使用

Inside your initialize method, you are not supposed to use

libraryNode = new TableView<Music>();

Controller的所有组件都标有 @FXML 在加载FXML时初始化

All the components of Controller marked with @FXML are initialized while the FXML is loaded

这篇关于TableView中没有内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆