# Problem with Java program?

This topic is 3695 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

code:

import javax.swing.*;

public class Button2 extends JFrame {
JButton Load , Save ,Subscribe , Unsubscribe;
ImageIcon load , save , subscribe , unsubscribe;
public Button2 (){
super("Button");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

save = new ImageIcon("save.gif");
subscribe = new ImageIcon("subscribe.gif");
JButton buttonsave = new JButton( "LoadIcon" ,save);
JButton buttonsubscribe = new JButton(" LoadIcon ",subscribe);

JPanel panel = new JPanel();

setVisible(true);
pack();
}

public static void main(String args[]){
Button2 a = new Button2();
}
}


The code brings up the three buttons but doesnt fully display the name and graphic. It's even worse without the "pack()" method. Maybe it's something about the images. I dont know but I would appreciate your help. Thanks

##### Share on other sites
Shouldn't a custom Button extend the class JButton instead of JFrame?

##### Share on other sites
It sounds like the images aren't actually being loaded. My guess would be that you don't have the right path to the file. You must have the entire path to the file in order for the ImageIcon to correctly load it.

So, you have two choices:

1. Manually add the entire path (not the relative path), which is the way you are doing it now. For example, if you put "subscribe.gif" in the base directory of your C drive, the path would be "C:\\subscribe.gif".

2. Load the pictures relative to the folder your class is in. So, if you have "subscribe.gif" in the same folder as your class, you would use the following code:

java.net.URL subscribeUrl = this.getClass().getResource("subscribe.gif");
subscribe = new ImageIcon(subscribeUrl);

I would suggest that you use the second.

One more thing, when you use JFrames you should always use "getContentPane().add()" to add a component to the frame. So instead of "add(panel)" you should have

Greggles

##### Share on other sites
Thanks for your guys' advice. The wierd thing is that the images are in the same folder as the source. I'll try adding the paths in.

Thanks again.