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?
 
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
 
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
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

 
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
 
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
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.