0

I have created the following code for a school project, a "password protector", just for fun, really. However, the problem I have is that the icon image does not appear, but instead the default java "coffee cup".

import javax.swing.*;

import java.awt.*;
import java.awt.event.*;

public class UserInterfaceGUI extends JFrame
{
    private static final long serialVersionUID = 1;
    private JLabel userNameInfo; // ... more unimportant vars.

    public UserInterfaceGUI()
    {
        this.setLayout(new FlowLayout());
        userNameInfo = new JLabel("Enter Username:"); // ... more unimportant var. declartions

        this.add(userNameInfo); // ... more unimportant ".add"s

        event e = new event();
        submit.addActionListener(e);
    }

    public static void main(String[] args)
    {
        //This icon has a problem \/
        ImageIcon img = new ImageIcon("[File Location hidden for privacy]/icon.ico");

        UserInterfaceGUI gui = new UserInterfaceGUI();
        gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        gui.setSize(400, 140);
        gui.setIconImage(img.getImage());
        gui.setTitle("Password Protector");
        gui.setVisible(true);
    }
}

Can someone tell me why this just shows the java coffee cup at the bottom of the screen and on the bar at the top of the window?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
just-max
  • 103
  • 2
  • 5
  • 2
    Not sure if java can read an ".ico" file. Try using a .jpg or .gif image. – camickr Apr 08 '15 at 15:35
  • Are you sure your ImageIcon is fully initialized (error on path?) @camickr you're right , see http://stackoverflow.com/questions/12185768/does-swing-support-ico-files – alias_boubou Apr 08 '15 at 15:36
  • This is not going to solve your problem, but consider using [`setIconImages`](http://docs.oracle.com/javase/8/docs/api/java/awt/Window.html#setIconImages-java.util.List-) instead, which allows to define images of different resolutions – Robin Apr 08 '15 at 15:44
  • Let me know the file is resource or in a folder out of project? Also the file you use must be smaller than "32*32" i think.. – crAlexander Apr 08 '15 at 15:57
  • @crAlexander *"Also the file you use must be smaller than "32*32" i think."* [Think again](http://stackoverflow.com/questions/18224184/sizes-of-frame-icons-used-in-swing). The icon size on some Windows machines reaches at least 40x40. – Andrew Thompson Apr 09 '15 at 08:29

1 Answers1

3

There are two likely problems here:

  1. Java is unlikely to support .ico files. The only types that can be relied on are GIF, PNG & JPEG.

    For all types supported on any specific JRE, use ImageIO.getReaderFileSuffixes() (but seriously, for app. icons stick to the 3 types with guaranteed support).
  2. The code is trying to load an application resource as a file, when it will likely be (or become) an that should be accessed by URL. See the embedded resource info. page for tips on how to form the URL.
Community
  • 1
  • 1
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433