Question

I am fetching the data from the DAO to the GUI level.

When I want to load the Table I get an empty table only with the right row count of the clicked db symbol:

enter image description here

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);
    }
Was it helpful?

Solution

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,

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. 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. This will repaint the inserted rows.

P.S: You can simply call fireTableDataChanged(); method also. But this method will repaint entire table. Prefer this method only if the entire table list is changed. Else you have respective fireXX methods for Insertion, Update, Delete.

OTHER TIPS

Probably issue is in getValueAt method, change like this

 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();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top