Java Swing How to - Change Icon in JFrame








Question

We would like to know how to change Icon in JFrame.

Answer

import java.awt.Image;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
// www  .j av a2  s .  c om
import javax.imageio.ImageIO;
import javax.swing.JFrame;

public class Main {

  public static void main(String[] args) throws Exception {
    URL url16 = new URL("http://www.java2s.com/style/download.png");
    URL url32 = new URL("http://www.java2s.com/style/download.png");

    final List<Image> icons = new ArrayList<Image>();
    icons.add(ImageIO.read(url16));
    icons.add(ImageIO.read(url32));

    JFrame f = new JFrame();
    f.setIconImages(icons); 
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    f.setSize(200, 100);
    f.setVisible(true);
  }
}