I am having difficulties with placing images on Buttons using Java Swing. I have placed all images in a package named icon. The frame is loading as well as the buttons. I cant seem to get the images on the buttons.
Can anyone point out my mistake please.

public Launch(){
        getContentPane().setLayout(new BorderLayout());

        desIcon = getImageIcon("Editor.gif");
        selIcon = getImageIcon("icon/small_hand.gif");
        delIcon = getImageIcon("icon/remove_icon.gif");
        roadIcon = getImageIcon("road.gif");
        rotateIcon = getImageIcon("rotate.gif");
        juncIcon = getImageIcon("junction.gif");

        buttonGroup = new ButtonGroup();

        gridBag = new GridBagLayout();


 private ImageIcon getImageIcon(String iconName) {
        return new ImageIcon(iconName);
        //return new ImageIcon(this.getClass().getResource(iconName));

The function getImageIcon is the one that needs to retrieve the pictures.


5 Years
Discussion Span
Last Post by varun45

If you guys need the setControl function code i can paste it here.Let me know please


This is a common symptom, and the usual cause is that Java is looking for the icon files in the wrong place.
new ImageIcon(iconName) will not throw an exception if there is a problem, it just returns null. If you pass null to a JLabel or JButton for the image icon that also does not throw an exception, it just doesn't display an image. So the net effect is that if there is a problem reading the file you get no error messages, you just don't see the image.

You can use this little fragment to see where Java is looking for your files:

java.io.File f= new java.io.File(iconName);
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.