Hi! I'm a novice to JAVA, have spent a day trying to add an icon to a JButton, a JLabel, etc. I have even run an original Sun program. Everything functions fine, but the icon is not shown. I am beginning to think my installation is incomplete. Thank you for your help! (cup.gif is in the same folder as my .java and .class files) ImageIcon bird = new ImageIcon("cup.gif"); JButton button2 = new JButton(cup); add(button2);
Actually the problem was the method being used. The add() method is to add one component to another. What you really want to use is the setIcon() method where the add() method was used in the original post. Using the Icon-overloaded of version of the JButton constructor works fine too, but I don't think any casting is necessary -- you can pass the ImageIcon directly.