Solved

screen refresh

Posted on 2000-05-12
17
424 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
In this post we will learn different types of Android Layout and some basics of an Android App.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

734 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