如何在JTable单元格中显示.gif类型的图像 [英] How can I show an image of type .gif in my JTable cell

查看:65
本文介绍了如何在JTable单元格中显示.gif类型的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个JTable,它显示后台作业的运行状态.如果作业正在运行的最后一栏,请说该行(正在运行的作业)的状态应显示一个正在进行中的.gif图像. 我的代码的问题是,它不显示图像类型gif.它虽然显示.png或.jpg. 我经历过各种论坛,但是没有一个论坛能帮助我解决问题.

I've a JTable which shows the running status of the background job. If a job is running the last column say status of that row(running job) should show a .gif image as in progress. The problem with my code is that, it does not show the image type gif. It shows .png or .jpg though. I've gone through various forums but none of them helped me top solve my problem.

以下是使用DefaultTableModel在表中添加一行的代码段:

Here is the code snippet to add a row in table using 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);  
}

在阿伯贝码中,如果图像类型不是.gif,它将显示在表格的第五列中.我为此使用了TableCellRenderer. 请以简单的解决方案回复.谢谢.

In abobe code if the image type is other than .gif it is showing in the fifth column of table. I've used TableCellRenderer for this. Kindly reply with a simple solution. Thanks.

推荐答案

我可以用3种主要格式的jpg,png和GIF格式的图片格式给您一些工作代码的安宁感

I can give you some peace of working code with 3 exmples of major formats as jpg, png and your looking for pic format as is GIF

在这里有它,并确保您有正确的路径,并且如果项目中的图片文件夹位于src文件夹中,则项目文件夹或src文件夹中的图片位于哪里,您必须在images/linux.gif之前将另一个目录路径添加为src/images/linux.gif

here you have it and make sure you have right paths and where is your folder with pictures in project folder or src folder if images folder is in src folder you have to add another catalog path before images/linux.gif as 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);
}

}

这篇关于如何在JTable单元格中显示.gif类型的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆