I am trying to get a custom icon onto my JFrame. I have the icon image in my project folder but I can not seem to get it to work.
I have also tried setIconImage(new ImageIcon(imgURL).getImage()); but it does not seem to work for me either.
Also, what is the typical dimensions for a custom icon
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
/**
* @author Curtis
*/
public class Favorites extends JFrame implements ActionListener
{
String[] styles = {"Big Band", "Country", "Pop", "Rock", "Rap"};
Font boxFont = new Font("Times New Roman", Font.BOLD, 14);
JLabel instruct = new JLabel("What is your favorite type of music?");
JComboBox music = new JComboBox(styles);
JTextField result = new JTextField(20);
final int WIDTH = 270;
final int HEIGHT = 125;
public Favorites()
{
super("Favorite Music");
setSize(WIDTH, HEIGHT);
setLayout(new FlowLayout());
instruct.setFont(boxFont);
JFrame.setDefaultLookAndFeelDecorated(true);
add(instruct);
add(music);
add(result);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
music.addActionListener(this);
Image icon = Toolkit.getDefaultToolkit().getImage("icnnote.jpg");
setIconImage(icon);
}
@Override
public void actionPerformed(ActionEvent e)
{
throw new UnsupportedOperationException("Not supported yet.");
}
}
You need to move the
setIconImage()above thesetVisible(). It displays the frame at the timesetVisible()is called.UPDATE: You also need to change your image to
getToolkit().getImage(getClass().getResource("iccnote.jpg"))