How to wait until all components painted?

Hi - I have searched for hours for an answer to this and have yet to find something so any help is much appreciated!
Ive posted some code below to simulate the problem im facing. This is a simple frame which has a couple of custom jPanels embedded within it.
When this is run, the first 3 lines i see are:         I want this comment to be the last thing output
        Now finished panel paint
        Now finished panel paintBut, as this output suggests, i want the "I want this comment to be the last thing output" line to be displayed after the two panels are completely painted on the screen (in real life i need to know my components are displayed before the program can move on to further processing). Of course, if you cause a repaint you see the "now finished panel paint" each time, but im not interested in this, im simply interested in how to overcome this when the frame is first created and shown.
Ive tried everything i can think of at the "// ***** What do i need to put here? ******" position but nothing seems to work... In short my question is how can i construct my frame and get it to ensure that all components are fully displayed before moving on?
Sorry if the answer to this is obvious to someone who knows these things, but its driving me mad.
Cheers
import javax.swing.*;
import java.awt.*;
public class Frame1 extends JFrame {
  public static void main(String[] args) {
    Frame1 frame1 = new Frame1();
    // ***** What do i need to put here? ******
    // to ensure that the System.err.. call below
    // is only executed once both panels paint methods
    // have finished?????
    System.err.println("I want this comment to be the last thing output");
    System.err.flush();
  public Frame1() {
    this.getContentPane().setLayout(new FlowLayout());
    MyPanel  p1 = new MyPanel(); p1.setBackground(Color.red);
    MyPanel  p2 = new MyPanel(); p2.setBackground(Color.green);
    this.getContentPane().add(p1, null);
    this.getContentPane().add(p2, null);
    this.pack();
    this.setVisible(true);
  class MyPanel extends JPanel {
    Dimension preferredSize = new Dimension(200,200);
    public Dimension getPreferredSize() { return preferredSize;  }
    public MyPanel () { }
    public synchronized void paintComponent(Graphics g) {
      super.paintComponent(g);  //paint background
      // do some irrelevant drawing to simulate a bit of work...
      for (int i=0; i<200; i+=20) {
        g.drawString("help please!",50,i);
      System.err.println("Now finished panel paint"); System.err.flush();
    } // end of MyPanel
  }

Thanks both for your replies.
When doing the "Runnable runnable = new Runnable(){public void run(){ frame1.paint(frame1.getGraphics());....." method i sometimes see
    Now finished panel paint
    Now finished panel paint
    I want this comment to be the last thing output       
    Now finished panel paint
    Now finished panel paintwhich is a pain.
As for making a new Runnable to do the system.err.println and then doing the SwingUtilities(invoke) on that runnable, that didnt work for me at all.
So... in the end, my solution was to have the paint method set a boolean when finished and the main program sleep while this is false
     // Dont want to sleep for say a few seconds as cant be sure
     //painting will be finished, must monitor state of painting ...
     //therefore use while loop
     while (!isOk) {  // isOk is true when both panel paint methods complete
         System.err.println("Waiting......");
         try{   Thread.sleep(300 /* wait some time */);  }
         catch(Exception e){}
       System.err.println("Thankfully this works");which is a kind of mixture of both your suggestions i guess!
Thanks for your help, much appreciated.

Similar Messages

  • How to wait until setPage(number) is finished

    SR 3-8311228061
    When calling getStrings, found I need to iterate the pages of the doc/xls/etc
    However Autovue Support tells me that setPage() is asynchronous
    I asked how to wait until setPage() is complete, they suggested I post the question here
    Note, at the time its called the viewer (desktop version 20.2.2) is on the task tray, but not rendering the document
    Its only loaded to extract the text
    here is the blip of code
    for( curPage = 1; curPage <= pages; curPage++)
    vueBean.setPage( curPage ); //its necessary to call setPage even for page 1
    //If setpage fails stop (per Daniel/Oracle)
    int thisPage = vueBean.getPage();
    if( curPage != thisPage )
    //This message never shows up
    String msg = String.format("Pages[%d] thisPage[%d] curPage[%d] ", pages, thisPage, curPage);
    AdeptApplication.INSTANCE.publishEvent("file-event", msg,
    new SystemEvent(this.fileName, "GETTEXT_PAGEFAILED"));
    break;

    The code you have is to know whether changing pages succeeded or not, and unless you are trying to set a page number that is out of the page count it will always succeed
    So, yes, your test is the same as if (false)
    Now, page loading is done in an asynchronous manner
    1. you ensure that the page (the container for all the page artifacts) is created, synchronously
    2. the page content are streamed on another thread
    3. once the page is loaded, an event is sent VueEvent(VueEvent.FILEEVENT, VueEvent.ONPAGELOADED)
    So you should be able to query for info after you received the page loaded event
    Now, word documents are an special kind of documents, so you need to be aware of some specific issues.
    The same way ms-word does not know the total number of pages until the ENTIRE document is loaded, AutoVue will not be able to notify you either.
    And as Word, AutoVue allows you to display the pages that are already loaded, and you may query for some of its contents too.  BUT you can not query for page info when the page is been loaded (or obviously not yet loaded)
    The loading is a 2 phase one, part is done on the server (native code) and part is on the java side (display)
    So you will not be able to iterate
    for( curPage = 1; curPage <= pages; curPage++)
    simply because page count is not known.  You need to query doc info more than once.
    If page count = -1, you need to iterate for each page and then, test whether setPage() != getPage() and/or page count has been updated.

  • In our program, we are concerned about reducing CPU work; how to wait until a boolean condition changes in a "while loop"?

    Is there a way to wait until a boolean condition changes (instruction is sent) in a "while loop" (because I want the program run until I press exit button)? Now it is consuming a lot of CPU because it is running the loop very fast waiting for instructions unless I stop the program.
    Thank you.

    Use /functions/time and dialog/wait until next millisecond multiple in the
    loop. Input 100ms.
    "mcdesm" wrote in message
    news:[email protected]..
    > In our program, we are concerned about reducing CPU work; how to wait
    > until a boolean condition changes in a "while loop"?
    >
    > Is there a way to wait until a boolean condition changes (instruction
    > is sent) in a "while loop" (because I want the program run until I
    > press exit button)? Now it is consuming a lot of CPU because it is
    > running the loop very fast waiting for instructions unless I stop the
    > program.
    >
    > Thank you.

  • How to wait until the 1st part of the processing is done

    Hi,
    I had a hard time to combine two parts of processing in one .vi file, and thus would like to get some advice/help through the forum.
    Attached is the partial finished vi file I made. You can see that it is composed of two parts. The upper part is to run a bat file (check_result.bat), which generates a result.txt file. The lower part is to use the result.txt file as an input file, and check if the key word called "test case passed" is included in the result.txt file or not. So I want to run the upper part firstly, and then run the lower part after the upper part is finished.
    However, I don't know how to connect the two parts in a right way that the lower part only starts running after the upper part is finished. Any comments/help is highly appreciated.
    Thank you in advance.
    Xuedong 
    Attachments:
    question.vi ‏49 KB

    The magic of dataflow!
    All you need is a data dependency between the two parts. Often critical parts of each section contain error terminals, so you can just string em along in the proper order and each part must wait until the previous node has finished.
    In your particular case, the read operation has no error input, so you can recruit any other input. Create a fake data dependency by strategically placing a small sequence frame containing common code. The sequence cannot start until all code that provides imputs has finished, serving your purpose.
    Of course it seems silly to even try to read the file if the previous node failed. Right? So simply place the second part inside a case and hook it up to the error output. Now the second part (1) waits until the first part has finished AND (2) execute only of the first part succeeded. No sequence needed.
    All clear?  
    Message Edited by altenbach on 02-07-2007 02:34 PM
    LabVIEW Champion . Do more with less code and in less time .
    Attachments:
    dataflow.gif ‏4 KB
    dataflow2.gif ‏4 KB

  • How to wait until an Entourage Schedule item completed

    I am trying to script the "Send & Receive All" operation in Entourage, and I have found that I can trigger the process off (from within my script) by:
    tell application "Microsoft Entourage"
    execute schedule "Send & Receive All"
    end tell
    However, the script continues to process while the scheduled actions take place.
    Is there any way to wait until the schedule actions complete and then process the rest of the script?
    Thanks
    Susan

    Glad to help, Susan
    I found out about "connection in progress" by looking at the properties of "application" in the Entourage dictionary:
    connection in progress boolean [r/o] -- Are there any network connections in progress?
    (In fact I searched through the dictionary for the phrase "connect".) The property is a boolean (true/false), and my script uses a "repeat while" loop to test for that boolean condition.
    As well as properties, the Entourage application object also has elements. These include "POP account", "IMAP account", "Exchange account" and "Hotmail account". These can be referred to "by numeric index, test". So if you pass a list of accounts to the "connect" command, Entourage will connect and download mail from those accounts. For example, if you have two POP accounts, this works:
    tell application "Microsoft Entourage" to connect to {POP account 1, POP account 2}
    AppleScript lists are contained in curly braces.
    But it's simpler (and less prone to error) to do this:
    tell application "Microsoft Entourage" to connect to every POP account
    Alternatively, the "test" lets you refer to individual accounts by name - a list isn't essential, despite what the dictionary says:
    tell application "Microsoft Entourage" to connect to POP account "My Account"
    There's many ways of skinning a cat.
    To answer your more general question, though, there is no one "user guide" for AppleScript. You can learn the about the basics of the language from the AppleScript Language Guide. But that will tell you nothing about how to script particular applications. This is because every developer, if they do implement AppleScript in their application (and lots don't), implements it in different ways.
    So as well as learning AS, for each application that you want to script you have to learn a slightly different set of commands. There's an awful lot of trial and error involved, but one way to learn is to look at scripts that others have written. There are good resources at Apple's own AppleScript page and at AppleScript Central among others. For specifically scripting Entourage, try Paul Berkowitz's scripts.
    Different people will also recommend different books. My recommendation is AppleScript: The Definitive Guide by Matt Neuberg, but again it's not application-specific.
    Hope this helps, at least to give you some pointers.
    H

  • URLLoader - how to wait until URL is loaded and then contine...

    Hello,
    I have litle problem because I don't know how to load
    external XML file, wait when loading is completed and after that
    continue with next code.
    Consider following situation. I have a function in
    actionscript, let's say, handleXML()
    function handleXML() {
    someURLRequest = new URLRequest("some XML file");
    someURLLoader.addEventListener("complete","LoadCompleted");
    someURLLoader.load(someURLRequest );
    Alert.show("Print Second")
    function LoadCompleted(event:Event) {
    someXML = XML(someURLLoader.data)
    Alert.Show("Print first")
    The problem is, when someURLLoader.load is called, Alert
    window with Print second is displayed and then "Print first" alert
    is displayed because flex perform action behind someURLLoader.load
    and then perform function LoadCompleted().
    I would like that first, when someURLLoader.load is called, flex
    wait until external XML is loaded, display "Prinf first" alert AND
    THEN continue with code and displat "Print second" alert.
    It is possible to do it someway??
    thanks for help

    Hi, thanks for answer, but I think it is the same:) So what
    happens if I change your code like this...
    srv = new HTTPService();
    srv.url = "some XML file";
    srv.resultFormat = "xml";
    srv.addEventListener( ResultEvent.RESULT, loadCompleted );
    srv.send();
    Alert.show("Print Second")
    function loadCompleted( event:ResultEvent ) : void
    someXML = XML(event.result);
    Alert.show("Print First")
    In this case, after calling srv.send(), next code in the
    fuction is performed, so the Alert "Print Second"" is dsplayed.
    After that, URL request is completed and "Print Second" appears. I
    think it is because data calls are asynchronous. Is it there a
    possibillity to wait after calling send() function, until the
    request is processed and then continue with code which the send()
    function was called from ? It means, first Alert.show("Print
    First") is executed and then Alert.show("Print Second").???

  • How to wait until html page is completely rendered

    I use a JEditorPane to display HTML and i want to print it to an image. so i tried to load the page (JEditorPane.setPage(URL url)) and wait until the property "page" is fired. Then i printed the component to an image.
    My problem is that the page wasn't completely loaded (rendered?) when i tried to print it.
    Is there a way to be notified when the page is completely rendered?
    thx in advance
    ~elchaschab

    However, you might consider setting the document property for it to be
    loaded synchronously.i've already used my own HTMLEditorKit for synchronous document loading.
    class MyHTMLEditorKit
    extends HTMLEditorKit
    public Document createDefaultDocument()
    Document doc = super.createDefaultDocument();
    ((HTMLDocument)doc).setAsynchronousLoadPriority( -1);
    return doc;
    You probably already played around with calling repaint() and
    validate() before printing.i tried to call repaint and validate before printing but
    still it doen't work. :(
    As a last resort, you could experiment with calling the printing
    routine written as a thread passed to invokeLater(), although there is
    no obvious reason why this would make a difference. i don't understand what i should do with invokeLater().
    could you please give me a short example?
    thx,
    elchaschab

  • Hi, I can't load the videos from youtube or other sites for 2 days now. It waits a long time to open the videos, or waits until all the video is downloaded. It wasn't like that until 2 days ago. How can I fix this problem? Thanks in advance.

    can't watch videos, waits a long time to open it, or it never opens it.

    I have taken it back to the Apple store genius bar, but they say they don't see anything wrong. Well unless you use it all day and experience the problems when they happen, you wont see anything wrong. But there are lots wrong with it. But this would be the same store as I purchased the phone. And they backed up my old Iphone 4, but were not able to get anything to load back onto my new phone. So, I lost pretty much everything. But over time, some of my contacts have started showing up, although i am still missing over 800 of them.

  • How do I view all Components the page of a PDF doc?

    I'm using the text touch-up tool to make a correction to a document which is auto-generated through an internal system as a very complex pdf document.
    The text disappears behind another component as if it's being pushed below another panel.
    Is there a tool which would allow me to view a PDF document in much the same way that Word has a tool.
    I'm not so much wanting to edit the document now but more would like to understand how these components are being placed together.
    Something that would allow me to see a document and see whether an item was text or whether an item was some type of graphic which cannot be edited.
    Thanks,
    Mike

    Unfortunately you would need to select all your previous posts and move them down at once.

  • How do you make java components paint to an image?

    I tried having a JPanel as one of my instance variables inside my class.
    In the paint method i called paint on the JPanel and passed in a graphics2D object created from an image.
    But JPanel does not paint on my image. How do I make it do that?
    Basically I want to do some effects on text, but do not want to be bothered about text layout. So I want my JLabel to paint its output on an image and then I can do the effects with the image.
    Thanks,

    Create an image, get it graphics and call the paint method of the label.
    Graphics g = image.getGraphics();
    label.paint(g);
    The label must be added to a container and visible.

  • How can I delete all components of Firefox 8 for Mac. Again...all components and hidden files. (YES, I did check the Moz web site but the page was essentially blank

    I have an unstable install of Mozilla firefox 8 and need to delete ALL files from my mac running OSX 10.7.2. I went to Mozilla web site and got this but it does not populate with content:
    http://support.mozilla.com/en-US/kb/Uninstalling%20Firefox?s=uninstall+firefox+mac&r=0&as=s#w_removing-user-data-and-settings
    But nothing appears at this URL.

    Please note I need all files removed including the hidden ones.
    thx

  • How can I delete all emails at once from IPhone?

    I want to be able to delete all of my emails at one time from my IPhone.  I have to mail accounts: Yahoo and Gmail.

    karolanne wrote:
    I want to be able to delete all of my emails at one time from my IPhone.  I have to mail accounts: Yahoo and Gmail.
    http://www.conferencesthatwork.com/index.php/technology/2014/01/how-to-delete-al l-mail-messages-from-iphoneipad-in-one-step/
    >> Open Inbox >> Edit >> Mark All >> Mark As Read
    >> Open Inbox >> Edit  >> Check/select the top message; it will highlight the move button.
    >> Press and hold the move button and uncheck the message that you had checked earlier.
    >> Lift all your fingers off from the ipad screen and leave it alone. Wait until all your messages pile up on the right hand portion of the screen (in ipad); iphone will give you the actual number of emails it has selected for the action. Now they are just waiting for your command to be moved ALL at once.
    >> Choose trash to delete all of them or any other folder where u want to move them, like one piece. Remember this will replicate your action on the server so you will ACTUALLY move them or delete them on the server and not just the ipad.
    Once you have moved all messages to the trash you can either leave them there for the scheduled cleaning or empty it right away by doing this: go to trash folder and touch Edit. The Delete All button shows up at the bottom of the screen. Hit it! You’re done!

  • Program needs to wait until BDC will get over

    Hi Experts,
    In my report program i am using BDC call transaction. Once this BDC action will get over after that i need todo some logic in my program. How to wait until BDC will get over?
    Anyone have idea?
        CALL TRANSACTION 'CK41' USING  zdata
                                OPTIONS  FROM g_s_options
                                MESSAGES INTO msg_tab.
        PERFORM call_processing USING wa_default-kalaid
                                         wa_default-klvar
                                         wa_default-kokrs
                                         wa_default-bukrs.
    Mohana

    Hi,
    Program gets over in BDC call transaction after call transaction statement.
    If you use call transaction statement as below,
      CALL TRANSACTION 'ME22' USING bdc_tab MODE 'N' UPDATE 'S'     MESSAGES INTO messtab.
    Here Update 'S' means update will be syncronouse so program will wait untill this BDC gets completed, if you will not select this statement then by default program may take update 'A' which is asyncronouse means BDC will be processed parallel to this program, please check.
    After this call transaction statement you can do code changes as per your requirment in the same program.

  • Need to authorise new laptop after old one crashed.  Have 5 computers already authorised and cannot wait until July for 12 month "Deauthorise all computers" to become available again.  How can I authorise this new laptop?

    Hello... I need to authorise a new laptop after my old one completely died (before I could deauthorise it!!).  I have 5 computers already authorised and cannot wait until July for the 12 month "deauthorise all computers" option to become available again.  Does anyone know how can I authorise this new laptop as one of the 5, in place of the old one?  Any help gratefully received!

    Go to one of the authorized computers and de-authorize it individually.
    The one year limitation is ONLY for the de-authorize all option.

  • I am trying to authorise my new macbook air and it won't let me. I try to de-authorise all the other computers and it says I can only do this once a year and have to wait until the 7th Feb. It does not give me an option to authorise this computer

    I am trying to authorise my new macbook air for i-tunes and it won't let me. I try to de-authorise all the other computers and it says I can only do this once a year and have to wait until the 7th Feb. It does not give me an option to authorise this computer

    Always remember to 'de-authorize' you computer when selling or sending for service. If sending for repairs you might not get the same mother board or HD back and some how this counts as an authorized computer against your count.
    MJ

Maybe you are looking for