2013-12-09 38 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)。请看看编辑过的代码。 –