• Post Reply Bookmark Topic Watch Topic
  • New Topic

Adding icon to label  RSS feed

 
John Morgan
Ranch Hand
Posts: 78
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to figure out how to add a checkbox icon image to the end of a label.  basically I want something that looks like: "Option Good (image of checked box)" or "Option Bad (image of box with X in it)"

I have both images I need in a folder called src/resources/checkedbox,jpg and src/resources/uncheckedbox,jpg but for some reason they do not want to display.

Here is the code I have so far... just not sure what I am doing wrong.
 
Paul Clapham
Sheriff
Posts: 22701
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That ImageIcon constructor is expecting a file name. Since the string you passed as a file name starts with a "/" character, the operating system interprets the name as an absolute path, i.e. it's relative to the root of the file system. But according to you, the "resources" folder is several levels deep in the file hierarchy, and that's why the file can't be found.

So one option you have is to provide the complete path to the file.

The other option you have is to provide a relative path (no "/" at the beginning). However that's going to be relative to the current working directory of the application, which is a risky choice because the CWD will be different when you run that code outside your IDE.
 
John Morgan
Ranch Hand
Posts: 78
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have tried it with "../resources/checkbox.jpg", "resources/checkbox.jpg" & "/resources/checkbox.jpg"

Directory structure is as follows

Project -
            |
            |- resources (this is where the images are)
            |
            |- views (this is my java files)
            |
            |- Common (this is where repeatable code is (database connections etc))
 
Rob Spoor
Sheriff
Posts: 21117
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try using ImageIcon(URL), where the URL is a resource URL returned by Class.getResource or ClassLoader.getResource.
 
Rob Camick
Ranch Foreman
Posts: 2821
15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Read the section from the Swing tutorial on How to Use Icons for the better way to load the image.

Just make sure your directory is on the classpath.
 
Paul Clapham
Sheriff
Posts: 22701
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree, using a resource is the way to go. Just one comment: the resource (the image) should be near the class file in the tree of files. Right now you have the resource in the same subtree as the source code; you'll need a copy of the resource in the same subtree as the compiled code. Perhaps your IDE makes such a copy when you build the project; I remember working with an IDE which did that.
 
John Morgan
Ranch Hand
Posts: 78
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Rob for the link... it made a whole lot more sense to me after that and I can get the image to display.

I have run into a different problem now though I need to label to says something like "Option: " and not icon... then once an option has been chosen in a different part of the application I need the label to say "Choice is good (insert image here)" or "Choice is bad (insert image here)"... When I tried to do a lblTgtGoodCase.setText("Good Case: ", imgCheckedBox, JLabel.LEFT); I get an error message: "The method setText(String) in the type JLabel is not applicable for the arguments (String, ImageIcon, int)" so I am unsure on what I need to change the setText to now.

Again thank you all for the help, I am close to being done with my application but some little nit noid things are left.
 
salvin francis
Saloon Keeper
Posts: 1644
37
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Morgan wrote:"The method setText(String) in the type JLabel is not applicable for the arguments (String, ImageIcon, int)"

Use setText to set the text and setIcon to set the icon.
Further information can be found here : https://docs.oracle.com/javase/8/docs/api/javax/swing/JLabel.html#setText-java.lang.String-
and here: https://docs.oracle.com/javase/8/docs/api/javax/swing/JLabel.html#setIcon-javax.swing.Icon-
 
salvin francis
Saloon Keeper
Posts: 1644
37
Eclipse IDE Google Web Toolkit Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As a side note, Swing is an advanced topic. Your above doubt suggests me that you need to practice more core java and understand methods before diving into swing.
The Java doc at https://docs.oracle.com/javase/8/docs/api/ is the prime document of reference. It shows each class and it's corresponding methods.
A method can accept one or more parameters. For example, setText() accepts a single parameter which is of type String. Similarly, the setIcon accepts a single parameter of type Icon.
When you create a JLabel, you use one of its constructors to construct it. the one you are using allows you to create a JLabel using text, icon as well as alignment in a single statement:
JLabel(String text, Icon icon, int horizontalAlignment)

Alternatively, you can write:
 
John Morgan
Ranch Hand
Posts: 78
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Salvin that worked. I tend to be more of a visual learner so I take small projects and learn from them one step at a time. This is one such project, a little bit of SQL, a little bit of Swing, a little bit of Windows builder, and different POJOs to give me a base before delving into each area more in depth.

Again, thank you for the suggestion.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!