• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Adding an image to a jLabel within a jPanel

 
Ranch Hand
Posts: 74
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok. I have struggled for weeks and tried every possible tutorial --but within netbeans 18, the coding will not set a series of images (slide format) within the jLabel1. My next and previous buttons do set the correct location and image but the jLabel1.setIcon(icon) doesn't work. I can system print the "icon" and it gives me the image file with its location. Just will not set the jLabel1 with that icon image. Any advice would be greatly appreciated. Below is the code I'm using. You can substitute the images.


 
Marshal
Posts: 28137
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My guess is that the size of the JLabel is zero. You don't assign its contents when you create it, so perhaps the layout manager fixes its maximum size at zero before you try to put images into it.
 
Saloon Keeper
Posts: 10649
83
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think Paul's on the right track. I had to hack at it to get it to work seeing as I don't have NetBeans. See line 67.
 
Paul Clapham
Marshal
Posts: 28137
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:I think Paul's on the right track.



JLabel has methods named getMaximumSize() and getPreferredSize() and getMinimumSize() which you can use to investigate how you have set up the JLabel.
 
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OP uses the AbsoluteLayout.jar as library (well, that does the GUI builder), and so the size and position are set in line 54.

I am on NetBeans 12.5 with java 17, and the code as supplied works fine for me. Problem with NetBeans 18, or the java-version you use?
 
Piet Souris
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And have you checked that the images are indeed where you think they are?
 
Piet Souris
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And did you check the value of 'basePath' (if you are using Windows)?
 
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Don't use a dummy catch block. How will you ever know if you have an Exception???


Don't use that constructor to create the Icon. Instead use:


ImageIO will generated an Exception if the image can't be found.

Actually you should be using the getResource() method to read the image. See: Loading images Using getResource()

Note that the image needs to be found on your classpath, so make sure you IDE is configured correctly.


Create the label with some default text just to make sure the label is displayed initially.


What is the point of those statements in your methods?

and tried every possible tutorial


Apparently you didn't try the Swing tutorial. I suggest you look at the trail table of contents and save the link for most Swing basics.

I would especially start with the section on layout managers and learn how to create your own GUI's and not rely on the IDE. You should NOT be using the AbsoluteLayout.,
 
Piet Souris
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nothing wrong with OPs code, even though you may not like it.

The only problem was (at least on my windows machine) that that user.dir thing gives a string with double backslashes. Adding the rest of the path with forwardslashes does not work. It takes a simple action to correct this.
 
Bill Melendez
Ranch Hand
Posts: 74
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@R0b:  Here's what I am coding. It's a multilayer jpanel with each panel having its own set of components. The  goal is to provide a training aide for technical folks.
Chirp-Snippets-Program.jpg
[Thumbnail for Chirp-Snippets-Program.jpg]
 
reply
    Bookmark Topic Watch Topic
  • New Topic