1

I can't get icons to appear on my button. I've copied this code from a book as I'm a beginner trying to grasp Java programming. Unfortunately I can't progress without getting this working, the other exercises are based on GUI images. I believe I've followed the exact steps, hopefully somebody can help!

Here is the simple code:

package Chapter13;

import javax.swing.*;

/** @author Chris */
public class TestButtonIcons extends JFrame
{
    public static void main(String[] args)
    {
        JFrame frame = new TestButtonIcons();
        frame.setTitle("ButtonIcons");
        frame.setSize(200, 100);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }

    public TestButtonIcons()
    {
        ImageIcon usIcon = new ImageIcon("image/usIcon.gif");
        ImageIcon caIcon = new ImageIcon("image/caIcon.gif");
        ImageIcon ukIcon = new ImageIcon("image/ukIcon.gif");

        JButton jbt = new JButton("Click it", usIcon);
        jbt.setPressedIcon(caIcon);
        jbt.setRolloverIcon(ukIcon);

        getContentPane().add(jbt);
    }
}

File Hierachy:

As you can see I've copied the "image" file twice as an attempt to try and debug this. All the images are in the "image" folder despite the photos not showing them all.

File Hierachy

skaffman
  • 398,947
  • 96
  • 818
  • 769
cworner1
  • 461
  • 4
  • 13
  • 2
    How do you start the application? From which folder? Since you're using a relative path I suspect the base path is wrong. – Thomas Dec 21 '11 at 14:10
  • From "Chapter 13" I normal right click the file "TestButtonIcons" and select run. – cworner1 Dec 21 '11 at 14:12
  • Possible duplicate of [Loading JPGs into Swing Apps](http://stackoverflow.com/questions/8462029/loading-jpgs-into-swing-apps) – trashgod Dec 21 '11 at 14:17
  • I'm not sure how to set the base path properly, but after your comment I went to the properties of "BookExercises" project and changed the TestButtonIcons and set it as the main class and then it worked! – cworner1 Dec 21 '11 at 14:18

3 Answers3

2

you need to create the image folder outside of the source package when you run from the netbeans while when you run directly executing the jar then you need to put the executable jar file and image folder in same folder or same place

Pratik
  • 30,639
  • 18
  • 84
  • 159
  • Perhaps that might work, but it goes against what I'm told in my book. Not that its a big problem. However I was interested in a solution rather than a "work-around". Turns out after Thomas commented, by changing the "BookExercises" properties and changing the TestButtonIcons as the main class it now works. – cworner1 Dec 21 '11 at 14:21
  • Thanks for helping anyway bud! Appreciate it. – cworner1 Dec 21 '11 at 14:21
1

It seems a bit of a problem with BookExcercises; that is not a normal project. A normal NetBeans project (you can see all in the Files tab to the right of the Projects tab): there is a src directory shown under Source Packages. Building a project fills a target directory. Now a build would put the .class files there and also copy your images. Run (project) would run with as class path the target directory. Run File however might not find the images in the target directory.

A solution depends on the BookExcercises project. You might create a new project for every excercise. Depends.

Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
1

If you are using a command Prompt to run your program, then simply paste this image folder alongside your chapter13 package folder(which contains your TestButtonIcons.class).

Or if you using some IDE, check where your compiled stuff goes. Again paste your image folder alongside your chapter13 package folder (which contains your TestButtonIcons.class). That will do the needful to run the program as you are expecting.

Regards

nIcE cOw
  • 24,468
  • 7
  • 50
  • 143