0

我正在从 DAO 获取数据到 GUI 级别。

当我想加载表时,我得到一个空表,只有单击的 db 符号的正确行数:

在此处输入图像描述

要加载我使用的元素:

playlistTable.addMouseListener(new MouseAdapter() {
    public void mouseClicked(MouseEvent e) {
    if (e.getClickCount() == 1) {
        JTable target = (JTable)e.getSource();
        int row = target.getSelectedRow();
        playlistTableModel.setPlaylists(playlistService.getMoviesOfPlaylist(row));
}

为什么表是空的?

更新

表型号代码:

public class PlaylistTableModel extends AbstractTableModel {

    /**
     * Generated UID
     */
    private static final long serialVersionUID = 9058516120594137393L;


    /**
     * List of playlist to be shown
     */
    private List<Playlist> playlist;

    public PlaylistTableModel(List<Playlist> playlist){
        this.playlist=playlist;
    }

    public int getColumnCount() {
        return 1;
    }

    public int getRowCount() {
        return playlist.size();
    }

    public Object getValueAt(int arg0, int arg1) {
        Playlist playlists = playlist.get(arg0);
        switch(arg1){
        case 0: return playlists.getName();
        default: return null;
        }
    }    

    public void setPlaylists(List<Playlist> playlist){
        this.playlist=playlist;
        fireTableDataChanged();//Aktualisiert Playlist automatisch
    }

    public void setPlaylist(Playlist playlists){
        playlist.add(playlists);
        fireTableRowsInserted(playlist.size()-1, playlist.size()-1);
    }


    public String getColumnName(int column) {
        if(column==0){
            return "Playlist";
        }
        else{
            return null;
        }
    }

    /**
     * Returns a movie from the list specified by the index
     * @param row index of the movie
     * @return movie
     */
    public Playlist getPlaylist(int row){
        return playlist.get(row);
    }

    @Override
    public boolean isCellEditable(int row, int col) {
        return true;
    }

    @Override
    public void setValueAt(Object aValue, int rowIndex, int columnIndex){
        if(columnIndex!=0){
            return;
        }    
        fireTableCellUpdated(rowIndex, columnIndex);
    }
4

3 回答 3

1

这个方法可能有问题,

public void setPlaylist(Playlist playlists){
    playlist.add(playlists);
    fireTableRowsInserted(playlist.size()-1, playlist.size()-1);
}

例如,如果我们得到大小为 20 的播放列表。然后在分配给您正在调用的表模型播放列表后,

fireTableRowsInserted(playlist.size()-1, playlist.size()-1);

这将减少到,

fireTableRowsInserted(19, 19);

但实际上发生的事情是我们不只是插入一行,而是 20 行。因此,正如@Ivan.Latysh在答案部分中所建议的那样,您需要从行数开始到行数结束调用 insert。这将repaint插入的行。

PS:您也可以简单地调用fireTableDataChanged();方法。但是这种方法将repaint整张桌子。仅当整个表列表发生更改时才首选此方法。否则,您有各自fireXX插入、更新、删除方法。

于 2012-12-09T04:32:50.620 回答
0

可能问题出在getValueAt方法上,像这样改变

 public Object getValueAt(int arg0, int arg1) {
        // since you use only one column     
        return playlist.get(arg0).getName();
 }
于 2012-12-08T15:00:34.403 回答
0

问题是您正在触发错误的更改事件

fireTableRowsInserted(playlist.size()-1, playlist.size()-1);

文档状态:

public void fireTableRowsInserted(int firstRow,
                              int lastRow)

Notifies all listeners that rows in the range [firstRow, lastRow], inclusive, have been inserted. 

因此,如果您想达到最佳状态,请执行以下操作:

public void setPlaylist(Playlist playlists){
  int firstRow = playlist.size();
  playlist.add(playlists);
  fireTableRowsInserted(firstRow, playlist.size()-1);
}

或者干脆重新绘制整个表格。不要忘记该表也可能重新计算其大小。

fireTableDataChanged();
于 2012-12-08T22:01:22.307 回答