Progressbar TableCellRenderer无法正常工作

时间:2013-07-26 15:18:14

标签: java swing event-dispatch-thread jprogressbar

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

那是我的TableModel:

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

1 个答案:

答案 0 :(得分:1)

知道了,问题是,当我设置Table的数据时,我使用的是fireTableStructureChanged而不是fireTableDataChanged