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();

        setControls();
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

 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.

thnx

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

for better help sooner please post an SSCCE, that demonstraded your issue with Icon, not your code

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);
System.out.println(f.getAbsolutePath());

Thankss :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.