TableModel - 没有在gui中显示任何内容吗?

时间:2012-12-08 14:47:26

标签: java swing jtable abstracttablemodel listselectionlistener

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

当我想加载表时,我得到一个空表,只有所点击的数据库符号的右行数:

enter image description here

加载我使用的元素:

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

3 个答案:

答案 0 :(得分: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插入的行。

P.S:您也可以简单地调用fireTableDataChanged();方法。但是这个方法将repaint整个表。仅在更改整个表列表时才首选此方法。另外,Insertion, Update, Delete.

fireXX种方法

答案 1 :(得分:0)

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

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

答案 2 :(得分: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();