Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp



Only 48 hours left in the trailboss' kickstarter!

New rewards and stretch goals. CLICK HERE!



  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Image doesnt shows propely in java swing  RSS feed

 
parampreet Singh
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello there,
I am trying too make small app that takes pics from directory and show images on JLabel when user click on next button.but i dont know why it doesn't work properly.actually when i click on Next it show image but i need to resize frame every time.Please look on that and let me know where i am wrong. sorry for silly English ;)


 
Rob Camick
Ranch Hand
Posts: 2752
11
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By default components have a size of (0, 0). So when you add a component to a frame there is nothing to paint. In order to give a component a size you need to invoke the layout manager. When you resize a frame the layout manager is automatically invoked so your component is give a size and location.

You can manually invoke the layout manager by using code like:



However, a better approach is the add the label to the GUI when you create the GUI. Then when you want to change the image of the label you just use:

label.setIcon( .... );

Now the label will invoke the layout manager automatically and the label will repaint itself.
 
Rob Camick
Ranch Hand
Posts: 2752
11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Crossposted: http://stackoverflow.com/questions/30817036/image-doesnt-shows-on-jlabel-when-click-on-next-button-using-java-swing

Quit cross posting. Its annoying to spend the time answering a question only to find out the exact same answer has already been given and you didn't bother to reply stating an answer has been found.
 
parampreet Singh
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks buddy,your solution works. about cross posting?, i will take care next time!
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!