How to add a button image with text or using the button image file nae

I am triing to incorporate images on a button in my code but I am getting an error.  The error on new Image line 1 is - Cannot instantiate the type .
The error on line 2 setImage - change to  alternatives.  What am I doing wrong?
I would like to incorporate the file text and I would also like to know how incorprate the text separately that is without using the file text.
Image buttonImage1 = new Image("images/Ok.jpg");
button1.setImage(buttonImage1);
button1.setText("Ok");

Open in new window

Vanavah EdwardsAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CEHJCommented:
Try
button1.setIcon(new ImageIcon("images/Ok.jpg"));

Open in new window

0
Vanavah EdwardsAuthor Commented:
I removed the other lines and only left the one you posted.  All errors has disappeared but I am getting no image.
0
CEHJCommented:
Make sure you're running in the correct directory to use that path
System.out.printf("My current directory is %s\n", System.getProperty("user.dir"));

Open in new window

0
OWASP: Avoiding Hacker Tricks

Learn to build secure applications from the mindset of the hacker and avoid being exploited.

Vanavah EdwardsAuthor Commented:
From the console - My current directory is C:\Users\Vanavah\Documents\WORKSPACE\LAYOUTS
My original setting was button1.setIcon(new ImageIcon("images/Bird.gif"));
Although I chage it to the attached code it doesn't seem to make a difference.

button1.setIcon(new ImageIcon("images/src/formslayout/images/Ok.gif"));

Open in new window

0
CEHJCommented:
That will only work if the following file exists


C:\Users\Vanavah\Documents\WORKSPACE\LAYOUTS\images\src\formslayout\images\Ok.gif

you need to get the relative path from LAYOUTS correct
0
Vanavah EdwardsAuthor Commented:
So the code from you which I set to button1.setIcon(new ImageIcon("images/Ok.gif")); still isn't working.  In the directory I have a folder named images that has in the gif files.  The full path of the folder that was printed is
C::\Users\Vanavah\Documents\WORKSPACE\LAYOUTS
The images are in this folder
:\Users\Vanavah\Documents\WORKSPACE\LAYOUTS\src\formslayout\images\
Usually, all I have to do in put ImageIcon("images/Bird.gif")


0
Vanavah EdwardsAuthor Commented:
Bear with me.  I noticed you mentioned get relative paths Correct.  Give an example code.
0
CEHJCommented:
>>
The images are in this folder
C:\Users\Vanavah\Documents\WORKSPACE\LAYOUTS\src\formslayout\images\
>>

In that case your code needs to be
button1.setIcon(new ImageIcon("src/formslayout/images/Ok.gif"));

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Vanavah EdwardsAuthor Commented:
Do I need to create an image icon with
buttonImage1 = createImageIcon("images/Ok.gif");
and pass it to a method to get the relative URL (see attached Snippet) which is what I do with my comboboxes)
protected static ImageIcon createImageIcon(String path) {
    	java.net.URL imgURL = ImageComboBox2.class.getResource(path);
        if (imgURL != null) {
            return new ImageIcon(imgURL);
        } else {
            System.err.println("Couldn't find file: " + path);
                return null;
        }
    }

Open in new window

0
CEHJCommented:
You would be much better creating a new package (assuming Eclipse) off the root called 'images', placing all images in there and doing the following, then your path will work in 'any' context

button1.setIcon(new ImageIcon(getClass().getResource("images/Ok.gif")));

Open in new window

0
CEHJCommented:
We cross posted, but your resource code is equivalent. Make sure your 'image' package is set up as i mentioned
0
Vanavah EdwardsAuthor Commented:
I changed to your path.  You are right.  It works. Isn't there a way I can just specify the images folder like how I do with my comboboxes without having to hard core the path.  I don't want to hard core the path because when  a distribution file is created the images folder is next to the folder in the eclipse package which is the standard procedure.  i usually just have to put ImageIcon("images/Bird.gif"
0
CEHJCommented:
>>Isn't there a way I can just specify the images folder like how I do with my comboboxes without having to hard core the path

I mentioned that at http:#37291578
0
Vanavah EdwardsAuthor Commented:
I am sorry for taking so long to reply, I was in a business meeting.  Thanks for your help all your recommendations work well.  Closing session
0
CEHJCommented:
:)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.