[英]How can I show an image of type .gif in my JTable cell
我有一个JTable,它显示了后台作业的运行状态。 如果作业正在运行的最后一列说该行的状态 (正在运行的作业)应显示为正在进行的.gif图片。 我的代码的问题是,它没有显示图像类型gif。 它显示.png或.jpg。 我经历过各种论坛,但没有一个帮助我解决我的问题。
以下是使用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。 请回答一个简单的解决方案。 谢谢。
我可以给你一些工作代码的和平与3个主要格式的例子jpg,png和你寻找pic格式,就像GIF一样
在这里,你有它并确保你有正确的路径,你的文件夹在项目文件夹或src文件夹中的文件夹在哪里如果图像文件夹在src文件夹中你必须在images / linux.gif之前添加另一个目录路径作为src / images / 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);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.