java Swing using JFrame.pack() repeatedly - how efficient is it?

In short I was wondering, if I call JFrame.pack() on a frame that is already sized, will it take a long time to analyse this or will it simply return immediately? I ask for efficiency reasons. There is a picture in my frame that is being updated many times a second within a loop. Now JFrame.pack() has to be called after at least the first picture is drawn to insure that the frame is the right size.

To handle this what I have (in pseudo code) is:

boolean flag = false
while (condition) {
    if (!flag) {
        flag = true;

Now I was wondering if there would be a problem if I just left out the check for the flag and always called frame.pack(). Could the program decide fast enough if the frame is already the correct size?


Tricky to estimate differences in performance as I am displaying images received on an RTP socket.

You are micro optimizing your code. Getting external data through a socket will always be slower than repainting a frame.

I am actually using a swing timer

You should NOT use the Timer to do the actual reading of the image on the socket. The GUI will block while the image is being read. You should be using a separate Thread to read the image. It will probably be better for you to use a SwingWorker and then publish() the images as they become available. Read the section from the Swing tutorial on Concurrency for more information.

just thought it would be easier to describe my question in terms of a while loop.

No, it is better to give the actual code as you can see your current code is incorrect. Don't make use guess what you are doing.

Need Your Help

Could not link labels with centroids in Kmeans,Sklearn

python numpy scikit-learn

How to know which label belongs to what centroids? The code below has produces the labels and centroids.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.