Discussion:
Where to put image or other files in NetBeans IDE 6.9
Tapas Bose
2010-07-21 09:07:27 UTC
Permalink
Hello everybody.
I am learning Java Swing. At present JButton. I am trying to add an image with button in the function settingButton(). I am unable to run the .jar file with the supplied image. I made a folder namely "resource" under the Project folder "Button Change", same level with "build", "dist" and others. I then have added that "resource" folder with my Project by right clicking on the project name "Button Change" in Projects window->Then Properties->Adding folder "resource" by browsing, as the "Source Package Folders" under the categories "Sources" and saving the changes. Then I put "Button-Add.ico" file into that "resource" folder. And trying to access the file by code :

Code:
button.setIcon(new ImageIcon("C:/Users/Tanmoy/Documents/NetBeansProjects/Button Change/resource/Button-Add.ico"));

But I cannot able to display that icon. What I am doing wrong? It will be very helpful to me. Thank you.
J***@bbraun.com
2010-07-21 21:08:17 UTC
Permalink
Do a check before that call like:

new File(<path>).exists()

Anyways at some point you'll need that to be within the jar itself.
Probably this will help:
http://download.oracle.com/docs/cd/E17409_01/javase/tutorial/uiswing/components/icon.html

Javier A. Ortiz Bultrón
Quality Assurance Engineer
B.Braun Medical, Inc.
972-466-5076 Phone




"Tapas Bose" <***@gmail.com>



07/21/2010 04:07 AM
Please respond to
***@netbeans.org


To
***@netbeans.org
cc

Subject
[nbusers] Where to put image or other files in NetBeans IDE 6.9






Hello everybody.
I am learning Java Swing. At present JButton. I am trying to add an image
with button in the function settingButton(). I am unable to run the .jar
file with the supplied image. I made a folder namely "resource" under the
Project folder "Button Change", same level with "build", "dist" and
others. I then have added that "resource" folder with my Project by right
clicking on the project name "Button Change" in Projects window->Then
Properties->Adding folder "resource" by browsing, as the "Source Package
Folders" under the categories "Sources" and saving the changes. Then I put
"Button-Add.ico" file into that "resource" folder. And trying to access
the file by code :

Code:
button.setIcon(new
ImageIcon("C:/Users/Tanmoy/Documents/NetBeansProjects/Button
Change/resource/Button-Add.ico"));

But I cannot able to display that icon. What I am doing wrong? It will be
very helpful to me. Thank you.






********************************************************************************
The information contained in this communication is confidential, may be
attorney-client privileged, may constitute inside information, and is intended
only for the use of the addressee. It is the property of the company of the
sender of this e-mail. Unauthorized use, disclosure, or copying of this
communication or any part thereof is strictly prohibited and may be unlawful.
If you have received this communication in error, please notify us immediately
by return e-mail and destroy this communication and all copies thereof,
including all attachments.
********************************************************************************
ael
2010-07-22 07:15:22 UTC
Permalink
I forgot to set an example.

Icon subIcon = new ImageIcon(getClass().getResource("/desktopapplication/images/folderfile.png"));

Hope it can help u.
Tapas Bose
2010-07-22 16:20:53 UTC
Permalink
What I am doing are :
Making a folder "resources" under /Project/src (or /Project/src/package).
Adding that folder into Project
And by the following Code :

Code:

URL defaultIconUrl = this.getClass().getClassLoader().getResource("togglebutton/resources/default.png");
Image defaultImage = new ImageIcon(defaultIconUrl).getImage();
int scaleWidth = 20;
int scaleHight = 20;
alphaButton.setIcon(new ImageIcon(defaultImage.getScaledInstance(scaleWidth, scaleHight, Image.SCALE_SMOOTH)));
betaButton.setIcon(new ImageIcon(defaultImage.getScaledInstance(scaleWidth, scaleHight, Image.SCALE_SMOOTH)));


I think this code snippet modifiable. I am glad to know any other way. Thank you.
ael
2010-07-22 23:04:14 UTC
Permalink
Try to debug if the default.png location is existing. I think this is the problem.

Resource Path.
togglebutton/resources/default.png


Code:

URL defaultIconUrl = this.getClass().getClassLoader().getResource("togglebutton/resources/default.png");
ael
2010-07-22 07:10:55 UTC
Permalink
Click the button go to properties then set the icon. simple!

Also dont use .ico jsut use .jpeg or .png

new ImageIcon command will convert .jpeg or .png to icon.
Loading...