Solved

screen refresh

Posted on 2000-05-12
17
419 Views
Last Modified: 2010-04-01
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?
0
Comment
Question by:jsimoni
  • 4
  • 4
  • 4
  • +3
17 Comments
 
LVL 7

Expert Comment

by:Sasha_Mapa
Comment Utility
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
 
LVL 19

Expert Comment

by:Jim Cakalic
Comment Utility
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
 
LVL 19

Expert Comment

by:Jim Cakalic
Comment Utility
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
 
LVL 16

Expert Comment

by:heyhey_
Comment Utility
> 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
 
LVL 7

Expert Comment

by:Sasha_Mapa
Comment Utility
me? Let me write a small example for you...  :-)
0
 
LVL 16

Expert Comment

by:heyhey_
Comment Utility
> 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
 
LVL 16

Expert Comment

by:heyhey_
Comment Utility
proxies :(

ok  - waiting ... :)
0
 

Expert Comment

by:attiasr
Comment Utility
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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 7

Expert Comment

by:Sasha_Mapa
Comment Utility
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
 
LVL 3

Expert Comment

by:darinw
Comment Utility
Reviewing question.

darinw
Customer Service
0
 
LVL 1

Accepted Solution

by:
jsimoni earned 100 total points
Comment Utility
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
 
LVL 3

Expert Comment

by:darinw
Comment Utility
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
 
LVL 3

Expert Comment

by:darinw
Comment Utility
Comment accepted as answer
0
 
LVL 7

Expert Comment

by:Sasha_Mapa
Comment Utility
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
 
LVL 16

Expert Comment

by:heyhey_
Comment Utility
calling validate() helps to solve some problems, but does not slove the question that has been posted

> until my process is complete
0
 
LVL 19

Expert Comment

by:Jim Cakalic
Comment Utility
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
 
LVL 3

Expert Comment

by:darinw
Comment Utility
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

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
count11 challenge 6 47
word0 challenge 4 52
backtracking recursion  code 19 39
eclipse java  build path 6 17
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now