Academic Java    Java Tutorial  >  Swing  >  JFrame.setIconImage()

JFrame.setIconImage() Example

Java JFrame.setIconImage EXAMPLE output Most platforms allow an image to be displayed in the top left of a JFrame window and also as the icon for the JFrame when it is minimized.

This is accomplished with the JFrame.setIconImage() method. The parameter for JFrame.setIconImage() is an Image. Setting the original image to a particular size is generally not necessary (the small red image shown top left in the JFrame is actually much larger than shown). Image formats can either be GIF, JPEG, or PNG.
// JFrame Icon Example
import java.awt.*;
import javax.swing.*;

class JFrameIcon {

   public static void main(String[] args) {

      final JFrame jframe = new JFrame("My JFrame with Icon");
      jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

      Image img = Toolkit.getDefaultToolkit().getImage("images/HK.gif");
      jframe.setIconImage(img);

      jframe.getContentPane().add(new JLabel("JFrame icon", SwingConstants.CENTER));

      jframe.setSize(200, 200);
      jframe.setVisible(true);
   }
}


* JFrame Examples