Search code examples
javaimageswingawtimageicon

Set icon to JFrame


I've tried the code below.

try {
        this.setIconImage(ImageIO.read(new File("/resources/dbs.ico")));
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        JOptionPane.showMessageDialog(null, e.getMessage());
        System.exit(0);
    }

Alsow I've tried the code given below.

yourFrame.setIconImage(Toolkit.getDefaultToolkit().getImage(getClass().getResource(Filepath)));

Also I've tried the code given below.

URL iconURL = getClass().getResource("/some/package/favicon.png");
ImageIcon icon = new ImageIcon(iconURL);
frame.setIconImage(icon.getImage());

But none of all these solution is working. The directory structure is :

/pro_root/com/girishpro/multirecharge/resources/dbs.ico

Also I've tried below.

/pro_root/resources/dbs.ico

The dbs.ico is the image which I want to set as Icon for JFrame. Please help me for solving this problem.


Solution

  • Use ImageIO.getReaderFileSuffixes() for an array of file types that the Java run-time can read. It might appear like this (actual example for this machine right now):

    type: bmp
    type: jpg
    type: wbmp
    type: jpeg
    type: png
    type: gif
    

    Nope. No ico type.