use the image proxy. Images are only loaded when you press on a tab : Image ImageIcon « Swing « Java Tutorial






import java.awt.Component;
import java.awt.Graphics;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTabbedPane;

public class ProxyTester {
  public static void main(String[] args) {
    JTabbedPane tabbedPane = new JTabbedPane();
    for (String name : imageNames) {
      JLabel label = new JLabel(new ImageProxy(name));
      tabbedPane.add(name, label);
    }

    JFrame frame = new JFrame();
    frame.add(tabbedPane);

    frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
  }

  private static final String[] imageNames = { "A.gif", "B.gif",
      "C.gif", "D.gif", "E.gif" };

  private static final int FRAME_WIDTH = 500;
  private static final int FRAME_HEIGHT = 300;
}

class ImageProxy implements Icon {
  public ImageProxy(String name) {
    this.name = name;
    image = null;
  }

  public void paintIcon(Component c, Graphics g, int x, int y) {
    ensureImageLoaded();
    image.paintIcon(c, g, x, y);
  }

  public int getIconWidth() {
    ensureImageLoaded();
    return image.getIconWidth();
  }

  public int getIconHeight() {
    ensureImageLoaded();
    return image.getIconHeight();
  }

  private void ensureImageLoaded() {
    if (image == null) {
      System.out.println("Loading " + name);
      image = new ImageIcon(name);
    }
  }

  private String name;
  private ImageIcon image;
}








14.117.Image ImageIcon
14.117.1.Getting Image object from ImageIcon object
14.117.2.GrayFilter: Create a disabled version of an ImageGrayFilter: Create a disabled version of an Image
14.117.3.use the image proxy. Images are only loaded when you press on a tab