Results 1 to 5 of 5

Thread: How to change JFrame image icon?

  1. #1
    Join Date
    Nov 2009
    Posts
    131

    How to change JFrame image icon?

    Hello friends,
    I am new to this forum. I am last year Computer Science student. As part of my syllabus I am working on live project. In that project I have to change JFrame image icon. I tried various method but none of them worked out. Can anyone tell me how to change JFrame image icon? Please help me.
    Thank you.
    Last edited by Juany; 08-02-2010 at 04:00 PM.

  2. #2
    Join Date
    Apr 2008
    Posts
    1,948

    Re: How to change JFrame image icon?

    Following program shows how to change a JFrame image icon. In this program I have use the setIconImage() method to do this. In this program I have use javax.imageio.ImageIO class to input image into our program. I also have use javax.swing.JFrame class to install frame into our code. To store image temporarily I have use java.awt.image.BufferedImage class.



    Code:
    package sample.code.java.example.swing;
    
    import javax.imageio.ImageIO;
    import javax.swing.JFrame;
    import java.awt.image.BufferedImage;
    import java.awt.Dimension;
    import java.io.IOException;
    
    public class FrameIconEg extends JFrame {
        public static void main(String[] args) {
            FrameIconExample frames = new FrameIconExample();
            frames.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            
            frames.setSize(new Dimension(350, 250));
            frames.setTitle("Example of Frame Icon");
    
            BufferedImage images = null;
            try {
                images = ImageIO.read(frames.getClass().getResource("/colors.jpg"));
            } catch (IOException es) {
                es.printStackTrace();
            }
            frames.setIconImage(images);
    
            
            frames.setVisible(true);
        }
    }

  3. #3
    Join Date
    May 2008
    Posts
    2,012

    Re: How to change JFrame image icon?

    Using following program you will bale to learn How to change JFrame image icon. In the following program I have use frame.setIconImage(Toolkit.getDefaultToolkit().getImage("car.gif")); method to change JFrame image icon. It is very simple program. You have to just use javax.swing.* class to do this. It is very simple program.




    Code:
    import javax.swing.*;
    import java.awt.*;
    
    public class SettingIconFrameEg
     {
      public static void main(String[] args){
        JFrame frames = new JFrame("Set an Icon for a frame");
        frame.setIconImage(Toolkit.getDefaultToolkit().getImage("car.gif"));
        frames.setDefaultCloseOperation(JFrames.EXIT_ON_CLOSE);
        frames.setSize(450,300);
        frames.setVisible(true);
      }
    }

  4. #4
    Join Date
    May 2008
    Posts
    2,297

    Re: How to change JFrame image icon?

    Hey it is very simple to change JFrame image icon. You have to just use following code in your project to do this. I have written followng code for you. In the following code I have use URL class to take image directly from the any website. I also have use mageIcon loadImageIcon(String filenames) method to take image from one folder and assigning to Jframe.


    Code:
    public static ImageIcon loadImageIcon(String filenames) {
    
        URL urls = Configs.class.getClassLoaders().getResources( IMAGE_DIR + filenames);
        if (urls == null) {
            System.err.println("image does't exits" + filenames);
            return null;
        }
        ImageIcon icons = new ImageIcon(urls);
        return icons;
    }

  5. #5
    Join Date
    Nov 2005
    Posts
    1,323

    Re: How to change JFrame image icon?

    I have written following program for you. It is very easy to change JFrame image icon. In this program I have use setIconImage method. It is used to set new icon for the jframe by replacing old image. In the following program I also have use javax.swing.JFrame; class to install JFrame in the project.



    Code:
    import javax.swing.JFrame;
    import javax.swing.ImageIcon;
    import java.awt.Image;
    
    public class ChangeJFrameIcon extends JFrame
    {
    ImageIcon icons;
    Image temps;
    
    public ChangeJFrameIcon()
    {
    super("changing JFrame Icon");
    
    
    icons=new ImageIcon("icons.png");
    temps=icons.getImage();
    
    
    setIconImage(temps);
    }
    
    public static void main(String[]args)
    {
    ChangeJFrameIcon aniss=new ChangeJFrameIcon();
    anis.setDefaultCloseOperation(JFrame)
    anis.setSize(350,500);
    anis.setVisible(true);
    }
    }

Similar Threads

  1. Replies: 4
    Last Post: 19-02-2012, 04:58 PM
  2. How to change icon or folder icon in Android
    By Tana$ya in forum Portable Devices
    Replies: 6
    Last Post: 06-07-2011, 10:35 AM
  3. JAVA: Image and Video in JFrame
    By Zia 7 in forum Software Development
    Replies: 4
    Last Post: 30-08-2010, 07:35 PM
  4. Display image icon in java
    By NetworkeR in forum Software Development
    Replies: 3
    Last Post: 14-11-2009, 01:19 PM
  5. Convert any image into icon
    By ChrisUlrich in forum Customize Desktop
    Replies: 2
    Last Post: 30-01-2009, 11:31 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,422,728.86299 seconds with 17 queries