Catching events while an image is being read using ImageIO

Add an IIOReadProgressListener to your reader.

   IIOReadProgressListener listener = new MyReadProgressListener();
   reader.addIIOReadProgressListener(listener);

It’ll throw events while the reading process is going on. The following example shows a JProgressBar while reading the image fruit.png, to be used as background.

Main.java:

import javax.imageio.metadata.*; 
import javax.imageio.stream.*;
import javax.imageio.event.*;
import javax.imageio.*;
 
import javax.swing.event.*;
import java.awt.image.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import java.awt.*; 
import java.io.*;
  
public class Main extends JFrame
{
   JProgressBar progressBar;
   BufferedImage background = null;
  
   public Main() {
      addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent we) {
            System.exit(1);
         }
      });
 
      progressBar = new JProgressBar(0, 100);
 
      getContentPane().setLayout(new BorderLayout());
      getContentPane().add(BorderLayout.NORTH, progressBar);
    }
 
   public void loadBackgroundImage(String filename) throws Exception {
      Iterator readers = ImageIO.getImageReadersByFormatName("png");
      ImageReader reader = (ImageReader) readers.next();
 
      IIOReadProgressListener listener = new MyReadProgressListener();
      reader.addIIOReadProgressListener(listener);
 
      ImageInputStream iis = ImageIO.createImageInputStream(new File(filename));
      reader.setInput(iis, true);
      background = reader.read(0);
   }
 
   public static void main(String []args) throws Exception {
      Main main = new Main();
      main.setSize(400, 150);
      main.setVisible(true);
  
      main.loadBackgroundImage("fruit.png");
   }
 
   public void paint(Graphics g) {
      System.out.println(background);
      if (background != null) {
         g.drawImage(background, 0, 0, getWidth(), getHeight(), this);
      }
   }
 
   class MyReadProgressListener implements IIOReadProgressListener {
 
	public MyReadProgressListener() {}
 
        public void imageProgress(ImageReader source, final float percentageDone) {
           System.out.println("imageProgress: " + percentageDone);
 
           SwingUtilities.invokeLater(new Runnable() {
              public void run() {
                 progressBar.setValue((int) percentageDone);
              }
           });   
        }
 
        public void imageComplete(ImageReader source) {
           SwingUtilities.invokeLater(new Runnable() {
              public void run() {
                 try {
                    // wait for a bit until bufferedimage is assigned by reader
                    Thread.sleep(50);
                 }
                 catch(InterruptedException e) { }
                 repaint();
              }
           });              
        }
 
        public void imageStarted(ImageReader source, int imageIndex)  { }
        public void readAborted(ImageReader source)                   { }
        public void sequenceComplete(ImageReader source)              { }
        public void sequenceStarted(ImageReader source, int minIndex) { }
        public void thumbnailComplete(ImageReader source)             { }
        public void thumbnailProgress(ImageReader source, float percentageDone) { }
        public void thumbnailStarted(ImageReader source, int imageIndex, int thumbnailIndex) { }
   }
}

Adding a JProgressBar inside a JTable cell

A JTable registers default renderers for a Number, Date, ImageIcon, Boolean and Object. If you want to render another component, you have to create a custom class that implements TableCellRenderer and register it with the method setDefaultRenderer on the JTable object.

The following example creates a JTable where every row displays data from a Download object. A download object is a thread that mimics the behavior of downloading a file by increasing the number of downloaded bytes (progress) with a random value. Using the Observable-Observer pattern, the download object notifies the table model whenever the progress of downloading has changed.

Main.java:

import javax.swing.table.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import java.awt.*;
 
public class Main extends JFrame {
   public Main() {
      super("TableModel JProgressBar Demonstration");
 
      // create our own custom TableModel
      DownloadTableModel downloadModel = new DownloadTableModel();
      JTable table = new JTable(downloadModel);
 
      // add rows to our TableModel, each row is represented as a Download object
      downloadModel.addDownload(new Download("linuxmandrake.zip", 1234567));
      downloadModel.addDownload(new Download("flash5.exe", 56450000));
      downloadModel.addDownload(new Download("jdk1.2.2-007.zip", 20000000));
 
      // render the columns with class JProgressBar as such
      ProgressBarRenderer pbr = new ProgressBarRenderer(0, 100);
      pbr.setStringPainted(true);
      table.setDefaultRenderer(JProgressBar.class, pbr);
   
      // increase the height of the rows a bit
      table.setRowHeight((int) pbr.getPreferredSize().getHeight());
 
      // create the scroll pane and add the table to it. 
      JScrollPane scrollPane = new JScrollPane(table);
 
      // add the scroll pane to this window.
      getContentPane().add(scrollPane, BorderLayout.CENTER);
 
      addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent e) {
            System.exit(0);
         }
      });
   }
 
   public static void main(String[] args) {
      Main main = new Main();
      main.pack();
      main.setVisible(true);
   }
}
 
// a simple object that holds data about a particular download
// it starts a thread and increases the progress of "downloading"
// in a random manner
class Download extends Observable implements Runnable {
   private Thread  thisThread;
 
   private String  filename;
   private int     filesize;
   private float   progress;
 
   public Download(String filename, int filesize) {
      this.filename = filename;
      this.filesize = filesize;
      progress = 0.0f;
      thisThread = new Thread(this);
      thisThread.start();
   }
 
   public String getFilename() { return filename; }
   public int    getFilesize() { return filesize; }
   public float  getProgress() { return progress; }
 
   public String toString() { 
      return "[" + filename + ", " + filesize + ", " + progress + "]"; }

   public void run() {
      Random r = new Random();
      int count = 0;
      while (count < filesize) {
         int random = Math.abs(r.nextInt() % 100000);
         count += random;
         if (count > filesize) count = filesize; 
         progress = ((float) count / filesize) * 100;
 
         // notify table model (and all other observers)
         setChanged();
         notifyObservers(this);       
 
         try { thisThread.sleep(500); } catch(InterruptedException e) { }
      }
   }
}
 
class DownloadTableModel extends AbstractTableModel implements Observer {
   // holds the strings to be displayed in the column headers of our table
   final String[] columnNames = {"Filename", "Filesize", "Progress"};
 
   // holds the data types for all our columns
   final Class[] columnClasses = {String.class, Integer.class, JProgressBar.class};
 
   // holds our data
   final Vector data = new Vector();
  
   // adds a row
   public void addDownload(Download d) {
      data.addElement(d);
   
      // the table model is interested in changes of the rows
      d.addObserver(this);
      fireTableRowsInserted(data.size()-1, data.size()-1);
   }
 
   // is called by a download object when its state changes
   public void update(Observable observable, Object o) {
      int index = data.indexOf(o);
      if (index != -1) 
         fireTableRowsUpdated(index, index);
   }
 
   public int getColumnCount() {
      return columnNames.length;
   }
         
   public int getRowCount() {
      return data.size();
   }
 
   public String getColumnName(int col) {
      return columnNames[col];
   }
 
   public Class getColumnClass(int c) {
      return columnClasses1;
   }
 
   public Object getValueAt(int row, int col) {
      Download download = (Download) data.elementAt(row);
      if (col == 0)      return download.getFilename();
      else if (col == 1) return new Integer(download.getFilesize());
      else if (col == 2) return new Float(download.getProgress());
      else return null;
   }
 
   public boolean isCellEditable(int row, int col) {
      return false;
   }
}
 
// a table cell renderer that displays a JProgressBar
class ProgressBarRenderer extends JProgressBar implements TableCellRenderer {
   public ProgressBarRenderer() {
      super();
   }
 
   public ProgressBarRenderer(BoundedRangeModel newModel) {
      super(newModel);
   }
 
   public ProgressBarRenderer(int orient) {
      super(orient);
   } 
 
   public ProgressBarRenderer(int min, int max) {
      super(min, max);
   }
 
   public ProgressBarRenderer(int orient, int min, int max) {
      super(orient, min, max);
   }
 
   public Component getTableCellRendererComponent(
      JTable table, Object value, boolean isSelected, boolean hasFocus,
      int row, int column) {
           
      setValue((int) ((Float) value).floatValue());
 
      return this;
   }
}

Setting the JProgressBar colors

You can set the progress rectangle color and remaining area color with setForeground and setBackground.
To set the color of the string drawn inside the progress bar, you have two choices (there is no trivial API method to set these colors, look at bug http://developer.java.sun.com/developer/bugParade/bugs/4226498.html.

  • Set the UIManager property values ProgressBar.selectionForeground and ProgressBar.selectionBackground before creating the JProgressBar. Beware that all your JProgressBar components will take on this color.
  • Extend the JProgressBar UI class to provide a method setSelectionForeground and setSelectionBackground

Main.java:

import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import java.awt.*;
 
public class Main extends JFrame {
   public Main() {
      super("JProgressBar Color Demonstration");
 
      // sets the color of the string painted inside the progressbar
      // selectionForeground is the color of the string that appears
      //    in the rectangle that shows the progress
      // selectionBackground is the color of the string that appears
      //    in the rectangle that shows the remaining area
      UIManager.put("ProgressBar.selectionForeground", Color.red);
      UIManager.put("ProgressBar.selectionBackground", Color.green);
 
      JProgressBar pb = new JProgressBar(0, 100);
      pb.setValue(50); 
      pb.setStringPainted(true);
 
      // sets the foreground color, but only the 
      // rectangle that shows the progress, not the text  
      pb.setForeground(Color.green);
 
      // sets the background color, the rectangle that  
      // shows the remaining area
      pb.setBackground(Color.red);
 
      // add the scroll pane to this window.
      getContentPane().add(pb);
 
      addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent e) {
            System.exit(0);
         }
      });
   }
 
   public static void main(String[] args) {
      Main main = new Main();
      main.pack();
      main.setVisible(true);
   }
}

Creating a JTable with a TableModel

A JTable can be passed the values the table should contain during construction, using a 2d array or a Vector. Creating a table with these constructors has some restrictions, like all data elements are rendered as String values or all cell elements are editable. To be more flexible, you can create your own custom TableModel that basically encapsulates the table data and provides some semantic knowledge about the data to be able to render it with an appropriate visual component. For example, you may wish to get the table values from a database as they become available or you might want to render a JProgressBar inside a table cell. Your custom table model should implement the interface TableModel, but you could simply extend from AbstractTableModel (a standard JDK class that implements TableModel) and override its methods.

The following example will create a simple table model that contains data about wines. The table is not editable but rows can be added by calling the method addWine to the model. Notice that this method contains a call to fireTableRowsInserted to tell the view to render the added row. Internally, the model uses a Vector to hold the data. The following API methods were implemented:

  • getColumnCount: called by the view to determine how many columns there are. The column names (headers) are hardcoded in an array. Return the length of this array.
  • getRowCount: called by the view to determine how many rows there are. As the vector contains our data, we return the size of the vector.
  • getColumnName: called by the view to render the column headers. Returns the element in our hardcoded header array.
  • getColumnClass: called by the view to determine what renderer to use for the specified column index. In the superclass, AbstractTableModel, this method returns Object.class, meaning it will treat and render all cells the same (as Objects, the toString() method is called on them when rendering). However, we’d like our member variable inStock to be rendered as a CheckBox instead of “true” or “false”. Therefore we return the Class of the column (hardcoded in the array columnClasses). This is enough for the view to know it should render column 4 as a JCheckBox instead of a JLabel. The data types Boolean, Number, ImageIcon and Object are recognized automatically and the view will use an appropriate cell renderer for them. Check out other questions in this category on how to create your own renderers eg. JProgressBar.
  • getValueAt: called by the view to determine a value at a particular row and column. From the row, we can get the Wine element out of our vector. The column determines what method to call on that object.
  • isCellEditable: called by the view to determine if a cell is editable or not. We return false in all cases.

Main.java:

import javax.swing.table.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import java.awt.*;
 
public class Main extends JFrame {
   public Main() {
      super("TableModel Demonstration");
 
      // create our own custom TableModel
      WineTableModel wineModel = new WineTableModel();
      JTable table = new JTable(wineModel);
 
      // add rows to our TableModel, each row is represented as a Wine object
      wineModel.addWine(new Wine("Chateau Meyney, St. Estephe", "1994", 18.75f, true));
      wineModel.addWine(new Wine("Chateau Montrose, St. Estephe", "1975", 54.25f, true));
      wineModel.addWine(new Wine("Chateau Gloria, St. Julien", "1993", 22.99f, false));
      wineModel.addWine(new Wine("Chateau Beychevelle, St. Julien", "1970", 61.63f, false));
      wineModel.addWine(new Wine("Chateau La Tour de Mons, Margeaux", "1975", 57.03f, true));
      wineModel.addWine(new Wine("Chateau Brane-Cantenac, Margeaux", "1978", 49.92f, false));
 
      // create the scroll pane and add the table to it. 
      JScrollPane scrollPane = new JScrollPane(table);
 
      // add the scroll pane to this window.
      getContentPane().add(scrollPane, BorderLayout.CENTER);
 
      addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent e) {
            System.exit(0);
         }
      });
   }
 
   public static void main(String[] args) {
      Main main = new Main();
      main.pack();
      main.setVisible(true);
   }
}
 
// a simple object that holds data about a particular wine
class Wine {
   private String  name;
   private String  vintage;
   private float   price;
   private boolean inStock;
 
   public Wine(String name, String vintage, float price, boolean inStock) {
      this.name = name;
      this.vintage = vintage;
      this.price = price;
      this.inStock = inStock;
   }
 
   public String getName()     { return name; }
   public String getVintage()  { return vintage; }
   public float  getPrice()    { return price; } 
   public boolean getInStock() { return inStock; }
 
   public String toString() { 
      return "[" + name + ", " + vintage + ", " + price + ", " + inStock + "]"; }
}
 
class WineTableModel extends AbstractTableModel {
   // holds the strings to be displayed in the column headers of our table
   final String[] columnNames = {"Name", "Vintage", "Price", "In stock?"};
 
   // holds the data types for all our columns
   final Class[] columnClasses = {String.class, String.class, Float.class, Boolean.class};
 
   // holds our data
   final Vector data = new Vector();
  
   // adds a row
   public void addWine(Wine w) {
      data.addElement(w);
      fireTableRowsInserted(data.size()-1, data.size()-1);
   }
 
   public int getColumnCount() {
      return columnNames.length;
   }
         
   public int getRowCount() {
      return data.size();
   }
 
   public String getColumnName(int col) {
      return columnNames[col];
   }
 
   public Class getColumnClass(int c) {
      return columnClasses1;
   }
 
   public Object getValueAt(int row, int col) {
      Wine wine = (Wine) data.elementAt(row);
      if (col == 0)      return wine.getName();
      else if (col == 1) return wine.getVintage();
      else if (col == 2) return new Float(wine.getPrice());
      else if (col == 3) return new Boolean(wine.getInStock());
      else return null;
   }
 
   public boolean isCellEditable(int row, int col) {
      return false;
   }
}