2013-12-09 44 views
0

我有一個JTable,它顯示後臺作業的運行狀態。如果某個作業正在運行,那麼該行(正在運行的作業)的最後一列狀態應顯示一個.gif圖像。 我的代碼的問題是,它不顯示圖像類型gif。它顯示了.png或.jpg。 我已經通過各種論壇,但沒有人幫助我解決我的問題。如何在我的JTable單元格中顯示類型爲.gif的圖像

這裏是代碼片段使用的DefaultTableModel在表中增加一個行:

for(ClassX obj: listOfClassX){ 
     Object[] objects = new Object[5]; 
     objects[0] = obj.getXX1(); 
     objects[1] = obj.getXX2(); 
     objects[2] = obj.getXX3() 
     objects[3] = obj.getXX4(); 
     objects[4] = new ImageIcon("../progress.gif"); 
     model.addRow(objects); 
} 

在的Abobe代碼,如果該圖像類型不是.gif注意它在表的第五列表示其他。我爲此使用了TableCellRenderer。 請回答一個簡單的解決方案。謝謝。

+0

* 「請回答一個簡單的解決方案。」*請問一個問題。 –

+0

閱讀[該帖子](http://stackoverflow.com/questions/14653967/animation-in-jtable) – alex2410

+0

Swing沒有問題顯示.gif,.png或.jpg文件。你的代碼應該沒有區別。只要您正在正確閱讀圖像,應顯示任何類型的圖像。看一個簡單的例子[Table Icon](http://stackoverflow.com/questions/5614875/how-to-set-icon-in-a-column-of-jtable/5615516#5615516)。 – camickr

回答

1

我可以給你用的主要格式爲JPG,PNG 3個exmples和你想找的PIC格式工作代碼的一些和平是爲​​GIF

在這裏你擁有了它,並確保你有正確的路徑和在哪裏與項目文件夾或src文件夾中的圖片文件夾,如果圖像文件夾位於src文件夾中必須添加之前的圖像/ linux.gif作爲SRC /圖片另一個目錄路徑/ linux.gif

public class AnimatedIconTableExample extends JFrame { 
private static final long serialVersionUID = 1L; 

public AnimatedIconTableExample() { 
super("AnimatedIconTable Example"); 

final Object[][] data = new Object[][] { 

    // Here is the looking for gif pictures 
    { new ImageIcon("images/game.gif"), 
     new ImageIcon("images/linux.gif") }, 

    // And here is the others pictures examples png and jpg 
    { new ImageIcon("images/folderGreen.png"), 
     new ImageIcon("images/apple.jpg") } }; 
final Object[] column = new Object[] { "Example image gif and png", 
    "Example image gif and jpg" }; 

AbstractTableModel model = new AbstractTableModel() { 
    public int getColumnCount() { 
    return column.length; 
    } 

    public int getRowCount() { 
    return data.length; 
    } 

    public String getColumnName(int col) { 
    return (String) column[col]; 
    } 

    public Object getValueAt(int row, int col) { 
    return data[row][col]; 
    } 

    public Class getColumnClass(int col) { 
    return ImageIcon.class; 
    } 
}; 

JTable table = new JTable(model); 
table.setRowHeight(50); 
setImageObserver(table); 
JScrollPane pane = new JScrollPane(table); 
getContentPane().add(pane); 
} 

private void setImageObserver(JTable table) { 
TableModel model = table.getModel(); 
int colCount = model.getColumnCount(); 
int rowCount = model.getRowCount(); 
for (int col = 0; col < colCount; col++) { 
    if (ImageIcon.class == model.getColumnClass(col)) { 
    for (int row = 0; row < rowCount; row++) { 
     ImageIcon icon = (ImageIcon) model.getValueAt(row, col); 
     if (icon != null) { 
     icon.setImageObserver(new CellImageObserver(table, row, 
      col)); 
     } 
    } 
    } 
} 
} 

class CellImageObserver implements ImageObserver { 
JTable table; 
int row; 
int col; 

CellImageObserver(JTable table, int row, int col) { 
    this.table = table; 
    this.row = row; 
    this.col = col; 
} 

public boolean imageUpdate(Image img, int flags, int x, int y, int w, 
    int h) { 
    if ((flags & (FRAMEBITS | ALLBITS)) != 0) { 
    Rectangle rect = table.getCellRect(row, col, false); 
    table.repaint(rect); 
    } 
    return (flags & (ALLBITS | ABORT)) == 0; 
} 
} 

public static void main(String[] args) { 
AnimatedIconTableExample frame = new AnimatedIconTableExample(); 
frame.addWindowListener(new WindowAdapter() { 
    public void windowClosing(WindowEvent e) { 
    System.exit(0); 
    } 
}); 
frame.setSize(300, 150); 
frame.setVisible(true); 
} 

}

+0

在我的情況下,我使用Object類型的一維數組來獲取ArrayList中一行的值並添加一行,如:model.addRow(objects)。請看看編輯過的代碼。 –