1

I'm only 13 and Java can be hard to understand. I need to change the icon in the top left hand corner on my screen but I can't figure it out. I have seen a lot of forum topics but don't understand it or where to put the method! Please Help!

Here is my code:

import javax.swing.JFrame;

public class Frame extends JFrame 
{
      public Frame()
      {
          //options for the frame  
          setTitle("Builder");
             setSize(1000, 650);
             setVisible(true);
             setDefaultCloseOperation(EXIT_ON_CLOSE);
             setResizable(true);
      }
//shortcut setting
      public static void main(String[] args)
       {
             Frame f = new Frame();
       }
}
bezmax
  • 25,562
  • 10
  • 53
  • 84
Ultrabyte
  • 227
  • 1
  • 4
  • 6

2 Answers2

7

Familiarize yourself with the javadoc. Use JFrame#setIconImage where the frame gets initialized. You can use

Image image = ImageIO.read(getClass().getResource("/images/icon.png"))
setIconImage(image);

Using getResource removes the reliance on the local file system and also allows images to be loaded from JAR files.

Reimeus
  • 158,255
  • 15
  • 216
  • 276
  • 2
    +1 for Java Doc. You could also use [`Window#setIconImages(List)`](http://docs.oracle.com/javase/7/docs/api/java/awt/Window.html#setIconImages%28java.util.List%29) and pass it a series of differently sized icons, allowing the system to choice the best icon for the OS - FYI – MadProgrammer Jun 03 '13 at 02:18
  • 1
    Also, shouldve mentioned `getResource` - its never too early to learn that one - will add :D – Reimeus Jun 03 '13 at 02:19
  • 1
    @MadProgrammer E.G. as seen in [File Browser GUI](http://codereview.stackexchange.com/questions/4446/file-browser-gui) which has a 16x16 or 32x32 icon available as needed. – Andrew Thompson Jun 03 '13 at 03:15
0
frame.setIconImage(myIcon);

Will do the job. myIcon is of type java.awt.Image

Mordechai
  • 15,437
  • 2
  • 41
  • 82