Solved

screen refresh

Posted on 2000-05-12
17
421 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
ID: 2805257
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
ID: 2805286
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
ID: 2805297
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
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
LVL 16

Expert Comment

by:heyhey_
ID: 2805347
> 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
ID: 2805373
me? Let me write a small example for you...  :-)
0
 
LVL 16

Expert Comment

by:heyhey_
ID: 2805376
> 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_
ID: 2805389
proxies :(

ok  - waiting ... :)
0
 

Expert Comment

by:attiasr
ID: 2807873
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
 
LVL 7

Expert Comment

by:Sasha_Mapa
ID: 2807969
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
ID: 2839412
Reviewing question.

darinw
Customer Service
0
 
LVL 1

Accepted Solution

by:
jsimoni earned 100 total points
ID: 2841097
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
ID: 2851040
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
ID: 2851046
Comment accepted as answer
0
 
LVL 7

Expert Comment

by:Sasha_Mapa
ID: 2853363
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_
ID: 2853369
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
ID: 2853921
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
ID: 2855972
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
groupSum6 challenge 6 86
java jdbc batch example 8 35
hibernate example using maven 12 42
mysql jsp example issue 32 38
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

803 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