Java TableModel-在gui中不显示任何内容?

Java TableModel-在gui中不显示任何内容?,java,swing,jtable,abstracttablemodel,listselectionlistener,Java,Swing,Jtable,Abstracttablemodel,Listselectionlistener,我正在将数据从DAO提取到GUI级别 当我想加载表时,我得到的是一个空表,其中只有单击的db符号的正确行数: 要加载我使用的元素,请执行以下操作: playlistTable.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent e) { if (e.getClickCount() == 1) { JTable target = (JTable)e.getSource(

我正在将数据从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);
    }
公共类playtablemodel扩展了AbstractTableModel{
/**
*生成的UID
*/
私有静态最终长serialVersionUID=905851612059413733L;
/**
*要显示的播放列表列表
*/
私有列表播放列表;
公共播放列表模型(列表播放列表){
this.playlist=播放列表;
}
public int getColumnCount(){
返回1;
}
public int getRowCount(){
返回playlist.size();
}
公共对象getValueAt(int arg0,int arg1){
Playlist playlists=Playlist.get(arg0);
开关(arg1){
案例0:返回播放列表。getName();
默认值:返回null;
}
}    
公共播放列表(列表播放列表){
this.playlist=播放列表;
fireTableDataChanged();//自动播放列表
}
public void setPlaylist(播放列表播放列表){
播放列表。添加(播放列表);
fireTableRowsInserted(playlist.size()-1,playlist.size()-1);
}
公共字符串getColumnName(int列){
如果(列==0){
返回“播放列表”;
}
否则{
返回null;
}
}
/**
*从索引指定的列表中返回电影
*@param电影的行索引
*@return电影
*/
公共播放列表getPlaylist(int行){
返回播放列表。获取(行);
}
@凌驾
公共布尔值可编辑(int行,int列){
返回true;
}
@凌驾
public void setValueAt(对象有效、int行索引、int列索引){
如果(列索引!=0){
返回;
}    
fireTableCellUpdated(行索引、列索引);
}

可能问题出在
getValueAt
方法中,如下更改

 public Object getValueAt(int arg0, int arg1) {
        // since you use only one column     
        return playlist.get(arg0).getName();
 }

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

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();

这个方法可能就是问题所在

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行。因此,正如在回答部分中所建议的,您需要从行计数的开始到行计数的结束调用insert。这将
重新绘制插入的行


p.S:您只需调用
fireTableDataChanged()
方法。但此方法将
重新绘制整个表。只有在整个表列表发生更改时才选择此方法。否则,您有相应的
fireXX
方法用于

调用后是否尝试重新绘制?现在我正在操作后重新绘制(请参见鼠标侦听器)我的意思是,尝试并调用重新绘制()除非您显示表模型的代码,否则没有办法提供帮助。将
列表
称为
播放列表
,并将单个播放列表实例称为
播放列表
,这非常令人困惑。为什么对单个实例使用复数形式,而对多个实例使用单数形式?同样,为什么要调用
setPlaylist()
添加播放列表的方法?