screen refresh

i have a process that runs and i would like to update a label on screen to alert the user of the status of this process. but as it is right now, the label isn't even drawn on screen until my process is complete.  i need help in forcing a screen refresh?
LVL 1
jsimoniAsked:
Who is Participating?
 
jsimoniConnect With a Mentor Author Commented:
thanks for the help, but i've resolved the issue on my own by placing a call to validate() after adding my label to the applet.  this forces my label to be drawn on screen and then it is repainted properly.
0
 
Sasha_MapaCommented:
I had the exact same problem. I was officially granted permission from heyhey_ (LOL) to use the following code:

Graphics g = myLabel.getGraphics();
if (g!=null)
  myLabel.update(g);

You can also of course open another thread and repaint your label once in a while from there.

Refer to the discussion at http://www.experts-exchange.com/jsp/qShow.jsp?ta=java&qid=10338321  for why this happens, when and what you can do to solve the problem.

Hope this helps, Sasha.
0
 
Jim CakalicSenior Developer/ArchitectCommented:
Are you using Swing? Does this "process" run on the Swing Event thread, perhaps within an actionPerformed method? If so, you should really be thinking of threads for long-running tasks. However, to help you overcome your immediate problem, I will recommend using the Component.paintImmediately() method. It requires a Rectange which you can get by calling Component.getBounds(). Here is a sample program which demonstrates the use (DISCLAIMER: I DO NOT RECOMMEND THIS PRACTICE SO PLEASE HOLD OFF THE FLAMES!)

---------- PaintTest.java ----------
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

class RestoreFrame implements Runnable {
    JFrame frame;

    public RestoreFrame(JFrame frame) {
        this.frame = frame;
    }
    public void run() {
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            frame.setState(Frame.NORMAL);
        }
    }
}

public class PaintTest {
    JFrame frame;

    public PaintTest() {
        frame = new JFrame("Paint test");
        frame.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
        final JLabel status = new JLabel("Status here");
        JButton begin = new JButton("Begin");
        begin.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent ae) {
                try {
                    for (int i = 0; i < 5; ++i) {
                        status.setText(Integer.toString(i + 1));
                        status.paintImmediately(status.getBounds());
                        Thread.sleep(1000);
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        frame.getContentPane().add(status, BorderLayout.NORTH);
        frame.getContentPane().add(begin, BorderLayout.SOUTH);
        frame.pack();
        frame.show();
    }

    public static void main(String[] args) {
        PaintTest app = new PaintTest();
    }

}
---------- end ----------

Best regards,
Jim Cakalic
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
Jim CakalicSenior Developer/ArchitectCommented:
Hmm. Sorry about that. The sample code I posted was "reused" from a previous problem I was working on. Ignore the non-public class RestoreFrame -- unless you want to look at it for an example of how to implement a Runnable ;-)

Jim
0
 
heyhey_Commented:
> officially granted permission

irony ? :)

I can only advice people. (you know my advice ' ... NEVER, NEVER ...' :)

I would like to see your code that really needs getGraphics() - can you send it to me or at least URL (heyhey_@iname.com)
0
 
Sasha_MapaCommented:
me? Let me write a small example for you...  :-)
0
 
heyhey_Commented:
> officially granted permission

irony ? :)

I can only advice people. (you know my advice ' ... NEVER, NEVER ...' :)

I would like to see your code that really needs getGraphics() - can you send it to me or at least URL (heyhey_@iname.com)
0
 
heyhey_Commented:
proxies :(

ok  - waiting ... :)
0
 
attiasrCommented:
From what you write in your question, the problem seems the following:
if what you call "process" is a flow of execution started by a user action, then
that code is in execution by the Event Handling thread. This is true for both
AWT and Swing. This thread is responsible for executing many methods, including
those to paint components. It should never be blocked or used to execute time
consuming operations. Here is an example:
suppose you want to provide a button in your application. When the user presses
the button, a file have to be read from disk. Now, if you call the method to read the file
directly  from the actionPerformed() method of the button (or a button listener), than such
method will be executed by the event notification thread. While the file is read, your screen
will not be updated. The solution is to spawn a new thread to execute time consuming operations.

     
0
 
Sasha_MapaCommented:
attiasr, I can see you are new to expert-exchange, so first of all welcome to the forum!
The procedure here is that you don't post an answer if you are not 100% sure that it will solve the questioner's problem, and you also don't do that if comments suggesting solutions were already posted. There is no way knowing which solution jsimoni will like better so posting an answer, unlike the rest of us who posted a comment seems inappropriate.
Please withsdraw your answer, that will make it a comment and will allow jsimoni to choose the comment/solution he likes best.

Sasha.
0
 
darinwCommented:
Reviewing question.

darinw
Customer Service
0
 
darinwCommented:
It looks like a very good solution for the Previously Asked Questions (PAQ) area.

-- I am refunding the points and accepting one of jsimoni's comments as an answer --

darinw
Customer Service
0
 
darinwCommented:
Comment accepted as answer
0
 
Sasha_MapaCommented:
darin, actually (and please Jim or heyhey correct me if I'm wrong), the solution posted by jsimoni would not solve the problem he posted initially.
0
 
heyhey_Commented:
calling validate() helps to solve some problems, but does not slove the question that has been posted

> until my process is complete
0
 
Jim CakalicSenior Developer/ArchitectCommented:
I agree. It is difficult to know exactly what the problem was without seeing the problem code. However, I don't see how simply calling validate could possibly have corrected the problem.
0
 
darinwCommented:
hmmm. That is not good. I do not want the PAQ here if the solution does not work.

jsimoni, can you elaborate on your solution using validat()?

darinw
Customer Service
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.