JButtons and pictures..

Ok, I don't know where the problem is; I'm trying to add an picture(gif picture) to a button.

The gif file is in the src folder (where all my .java classes are located); I even created an "images" folder and copied the gif file there (then I specified the location as "images/car.gif"), but the image won't show..the text shows in the button, but not the picture.

Here's a snippet of my code.

[code]ImageIcon icon = new ImageIcon( "car.gif" );
frameButton = new JButton( BUTTONS[ i ], icon );
frameButton.setVerticalTextPosition( AbstractButton.BOTTOM );
frameButton.setHorizontalTextPosition( AbstractButton.CENTER );[/code]

I've looked at many samples on the web, but I don't know what I'm missing that the icon won't show.


  • Maybe your are running your application from inside a
    Java Editor which changes the current application path.

    In netbeans before 5.0 when you run the application,
    it "runs" from /dist sub-folder, so, your .gif could be
    in the /src sub-folder and then ImageIcon doesn't find it.
    In eclipse it runs from the root folder, since some version,
    you can configure the /classes and /src in differente locations.

    /dist/images/car.gif <-- doesn't exist.
    /src/org/.../images/car.gif <-- there is your gif.

    Try to use a full path, /home/user/netbeans/.../car.gif if it shows
    then you are using a wrong image location. Or make a .jar
    and put the *.gif in the same folder and try to "java -jar" you
    application. Or configure you "run application path" in your
    editor. Note in windows should be "C:\My documents\..."

    Or your .gif is corrupt.
    [red]Good luck![/red]
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!


In this Discussion