• 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

resize icon to fill jLabel surface

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have successfully placed an imageicon onto a jLabel, however, I would like the dynamically changing image to fill the entire jLabel surface. Does anyone know how to manipulate the image to fill the entire jLabel surface?

The code below shows how I am dynamically updating the image. I created a separate thread to continuously loop and collect a byte array, which is then converted to an imageicon and finally placed on the jLabel. I selected jLabel because it seemed convenient to set the image. As long as the component can have a parent of a jPanel, I do not have an issue with changing the jLabel to another swing component if that component would better fit my needs.

Thanks in advance for your assistance.


 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You should read Concurrency in Swing. In this case, a javax.swing.Timer is a better choice than a Thread.
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You could use my Stretch Icon class. Note that the layout in which you place the JLabel must determine the label's size.

Or if you wanted to tile, not stretch, the image to fill the label, there's an Icon implementation that does that too, a few posts later in the blog.
 
Sabrina Cafey
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Darryl for pointing me to your Stretch Icon - I plan to look at your class next after I fix the threading issue in my code.

Thank you Rob for suggesting I read Concurrency in Swing. To clarify my understanding - If I were to create a Runnable thread as shown in the code example I provided in my previous post, I have a good chance of causing issues given how the Swing Thread model is constructed. The javax.swing.Timer class handles interaction with the event-dispatching thread (EDT), which removes the risk of issues. Hopefully this is correct so far?

I creating the following code and welcome comments/suggestion on whether this is a better approach or causes more issues. Not only am I dynamically updating an image on the jLabel approximately every 1-2 seconds, I am also expecting my user to click on a number of jButtons which will impact which dynamic image is displayed. Please correct me if I am wrong- The mouse clicks on the jButtons will be entered into the EDT to be handled in turn and the JLabel repaint request will be placed on the EDT as well. My concern is obtaining the dynamically created image does not hold off GUI responsiveness to the user clicking on the jButtons.
I am thinking by allowing the obtaining of the dynamic image to be done in a SwingWorker thread I can avoid this issue.

Please let me know if I have added undo complexity to this. If this approach is sound - I would appreciate feedback on where best to place the repaint of the jLabel.

Thanks again.




 
Rob Spoor
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sabrina Cafey wrote:Thank you Rob for suggesting I read Concurrency in Swing. To clarify my understanding - If I were to create a Runnable thread as shown in the code example I provided in my previous post, I have a good chance of causing issues given how the Swing Thread model is constructed. The javax.swing.Timer class handles interaction with the event-dispatching thread (EDT), which removes the risk of issues. Hopefully this is correct so far?


Correct.

I've seen your code, and I see one clear mistake - you cannot reuse a SwingWorker. A SwingWorker has a few states, and once it's done it can never be executed again. You'll need to create a new SwingWorker each time.
Your repaint in the done() method is good though. Two of SwingWorker's methods, process and done, are called on the EDT and can therefore interact with the GUI without any problems.

I'm also not sure if the invokeAndWait is going to work. I believe init() is called on the EDT, and you cannot call invokeAndWait from the EDT. That will lead to an error.
 
Sabrina Cafey
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for confirming my understanding and for reviewing my pseudo code. I plan to try working with SwingWorker more, however, decided that a Timer will work much better based on your comments and my readings. The following is my timer code for anyone that is reading this thread and needs an example.




I'm also not sure if the invokeAndWait is going to work. I believe init() is called on the EDT, and you cannot call invokeAndWait from the EDT. That will lead to an error.



I also found some information on the invokeAndWait method referenced above at this link: http://docs.oracle.com/javase/tutorial/uiswing/components/applet.html. In the "Threads in Applets" section it states

The invokeLater method is not appropriate for this implementation because it allows init to return before initialization is complete, which can cause applet problems that are difficult to debug.




 
Sabrina Cafey
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Darryl - Thank you for access to your Stretch Icon. My implementation has changed slightly and I do not currently have need for your stretch icon, however, I have book marked your link for future reference.
 
Normally trees don't drive trucks. Does this tiny ad have a license?
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic