aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes resize icon to fill jLabel surface Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "resize icon to fill jLabel surface" Watch "resize icon to fill jLabel surface" New topic
Author

resize icon to fill jLabel surface

Sabrina Cafey
Greenhorn

Joined: Sep 04, 2012
Posts: 5
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.


Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19696
    
  20

You should read Concurrency in Swing. In this case, a javax.swing.Timer is a better choice than a Thread.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4545
    
    5

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.


luck, db
There are no new questions, but there may be new answers.
Sabrina Cafey
Greenhorn

Joined: Sep 04, 2012
Posts: 5
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

Joined: Oct 27, 2005
Posts: 19696
    
  20

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

Joined: Sep 04, 2012
Posts: 5
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

Joined: Sep 04, 2012
Posts: 5
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: resize icon to fill jLabel surface