UCWare.com

UCWare IconTools for the Java platform

How to read an ICO icon resource from Java with UCWare IconTools?

If you need to read an ICO icon resource from Java, UCWare IconTools for the Java platform may come in handy.

UCWare IconTools lets you read an ICO icon resource into memory with just two lines of code:

    URL url = MyApp.class.getResource("my.ico");
    Icon[] icons = IconTools.readIcons(url);
    

As the result, you get an array of objects that implement the javax.swing.Icon interface. javax.swing.Icon allows for using icons throughout Swing (for instance, you can set icons on JButtons), and also in Java2D for obtaining BufferedImages, and so on.

The following code snippet will display all icons from the URL as buttons:

    contentPane = (JPanel) getContentPane();
    pane.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT));
    setTitle("DemoReader");

    URL url = MyApp.class.getResource("my.ico");
    Icon[] ic = IconTools.readIcons(url);

    setTitle("DemoReader - " + url.toExternalForm());


    for (int i = 0; i < ic.length; i++)
    {
        Icon icon = ic[i];
        IcoIcon iic = (IcoIcon) icon;
        String inf = iic.getIconWidth()+"x"+iic.getIconHeight()
                     +"@"+iic.getBiBitCount();
        pane.add(new JButton(inf, icon));
    }
    contentPane.add(pane);
    
Here is the on-screen result:

Javadoc documentation for UCWare IconTools is available here.

See also: Obtaining UCWare IconTools.