简体   繁体   English

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

[英]TableModel - Doesn`t show anything in the gui?

I am fetching the data from the DAO to the GUI level. 我正在从DAO到GUI级别获取数据。

When I want to load the Table I get an empty table only with the right row count of the clicked db symbol: 当我要加载表时,我得到的空表仅包含单击的数据库符号的正确行数:

在此处输入图片说明

To load the elements I use: 要加载我使用的元素:

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

Why is the Table empty? 为什么表为空?

UPDATE 更新

Table model code: 表模型代码:

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

This method may be the problem, 这种方法可能是个问题,

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

Here for example if we got the playlists with size 20. Then after assigning to the table model playlist you are calling, 例如,在这里,如果我们获得了大小为20的播放列表,然后将其分配给表模型播放列表后,

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

which will reduce to, 减少到

fireTableRowsInserted(19, 19);

But actually what has happened is we are not just inserting one row but 20 rows. 但是实际上发生的是,我们不仅要插入一行,还要插入20行。 So as suggested by @Ivan.Latysh in the answer section, you need to call insert from the start of the row count to the end of the row count. 因此,正如@ Ivan.Latysh在答案部分中建议的那样 ,您需要从行计数的开始到行计数的结束调用insert。 This will repaint the inserted rows. 这将repaint插入的行。

PS: You can simply call fireTableDataChanged(); PS:您可以简单地调用fireTableDataChanged(); method also. 方法也。 But this method will repaint entire table. 但是此方法将repaint整个表。 Prefer this method only if the entire table list is changed. 仅在更改整个表列表时,才建议使用此方法。 Else you have respective fireXX methods for Insertion, Update, Delete. 另外,您还有用于插入,更新和删除的 fireXX方法

Probably issue is in getValueAt method, change like this 可能问题是在getValueAt方法中,像这样更改

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

The issue is that you are firing a wrong change event 问题是您触发了错误的变更事件

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

Docs state: 文件状态:

public void fireTableRowsInserted(int firstRow,
                              int lastRow)

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

So if you want to be optimal, do this: 因此,如果您想获得最佳效果,请执行以下操作:

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

Or simply re-draw entire table. 或者只是简单地重新绘制整个表格。 Don't forget that table also may re-calculate its sizes. 别忘了该表也可能会重新计算其大小。

fireTableDataChanged();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM