Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Button and icon prob

Status
Not open for further replies.

wolfmah

Programmer
Oct 24, 2001
17
0
0
CA
I want to add an icon to a button but I got a problem, the following code doesn't work and I don't know why:

JToggleButton button = new JToggleButton( new ImageIcon( "images/oval.gif" ) );

My button is showing up, it does what I told it to do but the image is not showing up! The relative directory is existing and if I put some text in the initialisation, like this ( "some text", new ImageIcon //... ) it is showing up but the icon is still not there.

What am I doing wrong?
 
Have you tried the full path, like "C:\myImages\bla\bla.gif" ?
 
Yeah, when I type the full path it works, but this is not very portable from a comp to another since the path will never be the same. Is there anything I could do to make it more versatile?
 
... Well, if it works with the full path, but not with the relative path, then you can't be using the correct relative path. I'm not sure if Java cares whether you use / or \ to separate directory names, but you could certainly try playing with that: Remeber that on a Windows machine, it's \ to separate directories, and you'll need to use "images\\oval.gif" because \\ is the escape sequence for
Also, you need to make sure you're using the path relative to the directory that the application is being run from. (If you're really stuck, you could try just using the image name "oval.gif", and then copy the oval.gif file all over the place until it works)

If you're writing an Applet, then you'll need to retrieve the image resource from the correct place on the webserver (prob. somewhere near the applet), or from the JAR file that the applet is in (which is my preferred choice). (Remember that you can also have directories within a JAR file if you want)

I stuck an applet's class file, along with all it's images, in a JAR file... Here's the code I used to load the images:
Code:
...

public class CraneApplet extends JApplet{

 ...

  private JButton raiseButton = new JButton();

  ...

  public void init(){
 
    ...

    raiseButton.setRolloverEnabled(true);
    raiseButton.setIcon(
     new ImageIcon(
      getClass().getResource("raise_normal.gif"))
     );
    raiseButton.setRolloverIcon(
     new ImageIcon(
      getClass().getResource("raise_rollover.gif"))
     );
    raiseButton.setPressedIcon(
     new ImageIcon(
      getClass().getResource("raise_pressed.gif"))
     );

    ...

  } // End init()

...

} // End class CraneApplet
... Where all the images were in the same directory (in the JAR file) as the applet's class and manifest file, etc.

Hope it helps,
Stephen
 
BTW, I'm not making an applet but I have found my answer in your applet snippit. The only way I can make the icon appear whit a relative path is by using getClass().getResource("oval.gif").

I found this strange but anyway, thanks for your help, it's working now. :D
 
I guess it's not just an Applet thing then.

Glad I could help,
Stephen
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top