0

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.

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

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.

Balwant Kumar Singh
  • 1,158
  • 4
  • 24
  • 48
  • *"Kindly reply with a simple solution."* Kindly ask a question. – Andrew Thompson Dec 09 '13 at 12:43
  • read [that post](http://stackoverflow.com/questions/14653967/animation-in-jtable) – alex2410 Dec 09 '13 at 13:00
  • Swing has no problems display .gif, .png or .jpg files. There should be no difference in your code. As long as you are reading the image properly either type of image should display. See [Table Icon](http://stackoverflow.com/questions/5614875/how-to-set-icon-in-a-column-of-jtable/5615516#5615516) for a simple example. – camickr Dec 09 '13 at 16:31

1 Answers1

2

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

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

}

blueberry0xff
  • 3,707
  • 30
  • 18
  • In my case I'm using one dimension array of type Object to get the values of a row from ArrayList and adding a row like : model.addRow(objects). Plese see the code in question edited. – Balwant Kumar Singh Dec 10 '13 at 08:40