gpt4 book ai didi

java - TableModel - 图形用户界面中没有显示任何内容?

转载 作者:行者123 更新时间:2023-12-01 15:00:08 28 4
gpt4 key购买 nike

我正在将数据从 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);
}

最佳答案

这个方法可能有问题,

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 所建议的在答案部分,您需要从行计数的开始到行计数的末尾调用插入。这将重新绘制插入的行。

P.S:您也可以简单地调用 fireTableDataChanged(); 方法。但此方法将重新绘制整个表。仅当整个表列表发生更改时才首选此方法。否则,您有 Insertion, Update, Delete. 各自的 fireXX 方法

关于java - TableModel - 图形用户界面中没有显示任何内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13778561/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com