0

我正在尝试在 JTable 列上设置 JProgressbar 单元格渲染器。问题是,我的表格单元格上不会显示任何 ProgressBar,它只显示进度值。

那是我的表模型:

public class DownloadTableModel extends AbstractTableModel {

    private String[] columnNames = { "Song", "Artist", "Album", "Progress", "Size", "Path", "Date" };

    private List<Track> songDownloads = new ArrayList<Track>();

    public DownloadTableModel() {

    }

    public DownloadTableModel(List<Song> songs) {
        for (Song song : songs) { 
            Track track = Services.getDownloadService().download(song);
            songDownloads.add(track);
            fireTableDataChanged();
        }
    }

    @Override
    public int getColumnCount() {
        return columnNames.length;
    }

    @Override
    public int getRowCount() {
        return songDownloads == null ? 0 : songDownloads.size();
    }

    @Override
    public String getColumnName(int col) {
        return columnNames[col];
    }

    public Object getValueAt(int rowIndex, int columnIndex) {
        Track songDownload = songDownloads.get(rowIndex);
        switch (columnIndex) {
            case 0: return songDownload.getSong().getName();
            case 1: return songDownload.getSong().getArtist().getName();
            case 2: return songDownload.getSong().getAlbum().getName();
            case 3: return songDownload.getProgress();
            case 4: return songDownload.getSize();
            case 5: return songDownload.getPath();
            case 6: return songDownload.getDate();
        }
        return null;
    }

    public void setValueAt(Object obj, Track songDownload, int col) {
        int index = songDownloads.indexOf(songDownload);
        setValueAt(obj, col, index);
        fireTableCellUpdated(index, col);
        updateSongDownloads();
    }

    @Override
    public Class<?> getColumnClass(int column) {
        if(songDownloads.size() > 0 && getRowCount() > 0) {
            return getValueAt(0, column).getClass();
        } else {
            switch (column) {
                case 0: return String.class;
                case 1: return String.class;
                case 2: return String.class;
                case 3: return Integer.class;
                case 4: return String.class;
                case 5: return String.class;
                case 6: return String.class;
                default: return Object.class;
            }
        }
    }

    @Override
    public boolean isCellEditable(int row, int col) {
        return false;
    }

    public List<Track> getSongDownloads() {
        return songDownloads;
    }

    public void setSongDownloads(List<Track> songDownloads) {
        this.songDownloads = songDownloads;
        fireTableStructureChanged();
        updateSongDownloads();
    }

    public Track getSongDownload(Song song) {
        for(Track songDownload : songDownloads) {
            if(songDownload.getSong().equals(song)) {
                return songDownload;
            }
        }
        return null;
    }

    public int getSongDownloadIndex(Song song) {
        for(Track songDownload : songDownloads) {
            if(songDownload.getSong().equals(song)) {
                return songDownloads.indexOf(songDownload);
            }
        }
        return -1;
    }

    public void removeRow(int row) {
        songDownloads.remove(row);
        fireTableDataChanged();
        updateSongDownloads();
    }

    public void removeRow(Track songDownload) {
        songDownloads.remove(songDownload);
        fireTableDataChanged();
        updateSongDownloads();
    }

    public void addRow(Track songDownload) {
        songDownloads.add(songDownload);
        fireTableDataChanged();
        updateSongDownloads();
    }

    public void addRow(int row, Track songDownload) {
        songDownloads.add(row, songDownload);
        fireTableDataChanged();
        updateSongDownloads();
    }

    public void updateSongDownloads() {
        Main.getConfig().setDownloads(songDownloads);
    }

    public void fireTableCellUpdated(Song song, int col) {
        fireTableCellUpdated(getSongDownloadIndex(song), col);
    }

}

然后是 JTable:

downloadTable = new javax.swing.JTable();

downloadTable.setModel(new groovesquid.gui.DownloadTableModel());
downloadTable.getColumn("Progress").setCellRenderer(new ProgressCellRenderer());
downloadTable.setAutoCreateRowSorter(true);

downloadTable.setFocusable(false);

downloadTable.setGridColor(new java.awt.Color(204, 204, 204));

downloadTable.setRequestFocusEnabled(false);

downloadTable.setRowHeight(20);

downloadTable.getTableHeader().setReorderingAllowed(false);

jScrollPane2.setViewportView(downloadTable);

还有我的 TableCellRenderer:

public class ProgressCellRenderer extends JProgressBar implements TableCellRenderer {
    ProgressCellRenderer() {
        super(0, 100);
        setValue(0);
        setString("initializing...");
        setStringPainted(true);
    }

    @Override
    public boolean isDisplayable() { 
        // This does the trick. It makes sure animation is always performed 
        return true; 
    }

    @Override
    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
        String text = "";
        Color foregroundColor = Color.BLACK;

        this.setForeground(foregroundColor);
        this.setValue((Integer)value);
        this.setString(text);

        return this;
    }
}
4

1 回答 1

1

明白了,问题是,我在使用它fireTableStructureChanged而不是fireTableDataChanged在设置表格数据时使用它。

于 2013-07-27T12:49:22.420 回答