0

Here is my Code in java that i have written. A screen shot of my Laptop is attached which is showing output in which java icon is still there on (left corner).

import javax.swing.*;![enter image description here][1]
import java.awt.*;

public class Frame
{
    public static void createWindow()
    {
        JFrame frame = new JFrame("Warning");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JLabel textLabel = new JLabel ("Congratulation!! Installation Complete.",      SwingConstants.CENTER);
        textLabel.setPreferredSize(new Dimension(420,140));
        frame.getContentPane().add(textLabel, BorderLayout.CENTER);
ImageIcon img = new ImageIcon("D:\\Icons\\icon.ico");
        frame.setIconImage(img.getImage());


        frame.pack();
        frame.setVisible(true);
        frame.setLocationRelativeTo(null);
    }

    public static void main (String[] args)
    {
        createWindow();
    }

}

Ali Mohsan
  • 326
  • 2
  • 15

2 Answers2

1

..Icons\\icon.ico

Does the JVM in question even support .ico files? It is recommended to stick to PNG, GIF and JPEG. Here is the list1 of supported file types using Java 1.8 on a Windows 7 machine.

Reader  jpg
Reader  bmp
Reader  gif
Reader  png
Reader  jpeg
Reader  wbmp
Writer  jpg
Writer  bmp
Writer  gif
Writer  png
Writer  wbmp
Writer  jpeg

No .ico listed..

  1. Information obtained from the MediaTypes code seen in this answer.
Community
  • 1
  • 1
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
0

Try using the following if the link don't work fine to you. It's working for me:

use setIconImage(...).

import java.awt.Image;
import javax.swing.*;
import javax.imageio.ImageIO;

import java.net.URL;
import java.util.*;
//upto here just importing. Don't worry, let the eclipse or your editor do that for you
class FrameIcons {

    public static void main(String[] args) throws Exception {
        URL url16 = new URL("image.png");//Making two objects of URL class
        URL url32 = new URL("images.png");//second object of URL

        final List<Image> icons = new ArrayList<Image>();//this is just an arraylist of `icon. For now on if you don't know about list, think it as array where you can put element using .add() method`
        icons.add(ImageIO.read(url16));//adding to list
        icons.add(ImageIO.read(url32));//adding to list

        SwingUtilities.invokeLater( new Runnable() {//this is just for running a `runnable`
            public void run() {
                JFrame f = new JFrame("Frame Icons");//setting JFrame
                f.setIconImages(icons);//setting icon images
                f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//you must know `everything from here`
                f.setLocationByPlatform(true);
                f.setSize(200,100);
                f.setVisible(true);
            }
        });
    }
}
Nabin
  • 11,216
  • 8
  • 63
  • 98