Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    Codeasaurus Rex
    Join Date
    Jun 2008
    Location
    Redmond, WA
    Posts
    659
    Thanks
    31
    Thanked 100 Times in 94 Posts

    Components not repainting until function completes

    Hello all!

    I have a function in a Java applet that responds on a button press.

    I have 4 actions basically to the tune of "Please wait" and then a function that generates a large integer. This function can take a long time, depending on the input.

    Unfortunately, for some reason, Java is not doing the repaint until that function completes. This is bad because I want to let the user know something is happening, not just an application seeming to freeze.

    Any ideas on how I can force it to repaint after the components? I have added a block of failed attempts at the end of the post.

    Thanks!

    Code:
    private void btnGenerateKeysActionPerformed(java.awt.event.ActionEvent evt) {
            // Disable the components, display status text
            btnGenerateKeys.setEnabled( false );
            spinSetBitStr.setEnabled( false );
            slideSetBitStr.setEnabled( false );
            lblStatusText.setText( "Generating Keys, please stand by." );
    
            // Generate Keys
            rsa.generateRSAKeys( slideSetBitStr.getValue() );
    
            paneFirstPrime.setText( rsa.getP() );
            paneSecondPrime.setText( rsa.getQ() );
    
        }
    Code:
            // ....
            lblStatusText.setText( "Generating Keys, please stand by." );
    
            this.repaint();
    
            rootPane.revalidate();
            rootPane.updateUI();
            rootPane.revalidate();
            rootPane.repaint();
            SwingUtilities.updateComponentTreeUI( rootPane );
            SwingUtilities.updateComponentTreeUI( this );
    
            rootPane.revalidate();
            this.repaint();
            rootPane.repaint();
    
            rootPane.revalidate();
    
            // Generate Keys
            rsa.generateRSAKeys( slideSetBitStr.getValue() );
            //....
    Unless otherwise stated, any code posted is most likely untested and may contain syntax errors.
    My posts, comments, code, and suggestions reflect only my personal views.
    Web Portfolio and Code Snippets: http://shanechism.com

  • #2
    Senior Coder
    Join Date
    Dec 2005
    Location
    Slovenia
    Posts
    1,959
    Thanks
    120
    Thanked 76 Times in 76 Posts
    1. why don't you put function that takes a lot of time into separate thread ?
    2 I cant belive it takes a lot a time, check this:
    http://www.cs.princeton.edu/introcs/.../RSA.java.html
    Found a flower or bug and don't know what it is ?
    agrozoo.net galery
    if you don't spot search button at once, there is search form:
    agrozoo.net galery search

  • #3
    Codeasaurus Rex
    Join Date
    Jun 2008
    Location
    Redmond, WA
    Posts
    659
    Thanks
    31
    Thanked 100 Times in 94 Posts
    Quote Originally Posted by BubikolRamios View Post
    1. why don't you put function that takes a lot of time into separate thread ?
    2 I cant belive it takes a lot a time, check this:
    http://www.cs.princeton.edu/introcs/.../RSA.java.html
    The way I've gotten in working in the mean time is in fact to put it in a thread. I felt, however, that it seems a bit inefficient when a procedural implementation should, in theory, work.

    As for taking time, believe me - it does. My program's logic is similar to the script you mentioned, however a secure key is around 4,096 bits. Performing calculations on that large of a number can take around four to five seconds. As your key size increases, so does your processing time.

    Feel free to check it out for yourself: http://shanechism.com/code/2011/01/j...ation/download

    That application is based on this code: http://shanechism.com/code/2010/12/j...implementation
    Last edited by ShaneC; 01-06-2011 at 09:37 PM.
    Unless otherwise stated, any code posted is most likely untested and may contain syntax errors.
    My posts, comments, code, and suggestions reflect only my personal views.
    Web Portfolio and Code Snippets: http://shanechism.com

  • #4
    Senior Coder
    Join Date
    Dec 2005
    Location
    Slovenia
    Posts
    1,959
    Thanks
    120
    Thanked 76 Times in 76 Posts
    Yeah, if you would have multicore processor it might work without thread, cant tell for sure.

    Two things:
    1. Upon clicking generate, all result fields should be cleared
    2. An abort button would be nice.

    Regards
    Found a flower or bug and don't know what it is ?
    agrozoo.net galery
    if you don't spot search button at once, there is search form:
    agrozoo.net galery search

  • #5
    Codeasaurus Rex
    Join Date
    Jun 2008
    Location
    Redmond, WA
    Posts
    659
    Thanks
    31
    Thanked 100 Times in 94 Posts
    Quote Originally Posted by BubikolRamios View Post
    Yeah, if you would have multicore processor it might work without thread, cant tell for sure.

    Two things:
    1. Upon clicking generate, all result fields should be cleared
    2. An abort button would be nice.

    Regards
    Thank you for the suggestions! I'll look into implementing those down the line.

    As for the main issue, tragically I still haven't found a resolution. I've made some improvements to the threads to make them more efficient however I still feel some type of procedural method still exists.

    If anyone has any ideas please let me know. They would be much appreciated!
    Unless otherwise stated, any code posted is most likely untested and may contain syntax errors.
    My posts, comments, code, and suggestions reflect only my personal views.
    Web Portfolio and Code Snippets: http://shanechism.com

  • #6
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    This is typical behaviour of languages like java. You are essentially wanting to animate the frame by repainting it during other processing. When this processing is occurring, the script will halt and wait for its process to complete before continuing.
    As mentioned, the only way to do the updating while something is happening is to use a new thread. Even a simple run of just repaint I believe will update that window for you.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • Users who have thanked Fou-Lu for this post:

    ShaneC (01-07-2011)

  • #7
    Codeasaurus Rex
    Join Date
    Jun 2008
    Location
    Redmond, WA
    Posts
    659
    Thanks
    31
    Thanked 100 Times in 94 Posts
    Blast! Though, I can't say I'm too surprised. When Googling turns up so little results usually that means I'll end up being outta luck.

    As I said earlier the thread implementation did work, so thankfully I'm not up the creek. More than anything I was surprised that repaint(); didn't do the trick (at least in procedural). You can see all the methods I tried at the end of the first post.

    At any rate, thanks for the input!
    Unless otherwise stated, any code posted is most likely untested and may contain syntax errors.
    My posts, comments, code, and suggestions reflect only my personal views.
    Web Portfolio and Code Snippets: http://shanechism.com

  • #8
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Quote Originally Posted by ShaneC View Post
    Blast! Though, I can't say I'm too surprised. When Googling turns up so little results usually that means I'll end up being outta luck.

    As I said earlier the thread implementation did work, so thankfully I'm not up the creek. More than anything I was surprised that repaint(); didn't do the trick (at least in procedural). You can see all the methods I tried at the end of the first post.

    At any rate, thanks for the input!
    You betcha, if I can find some of the old stuff I did in school I can post that up for you; all I remember for sure is we needed to multithread it and I can't quite remember what I had to do. Man I need to do more gui work :S
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #9
    Senior Coder
    Join Date
    Dec 2005
    Location
    Slovenia
    Posts
    1,959
    Thanks
    120
    Thanked 76 Times in 76 Posts
    was just banging my head against the wall about same thing,wasting a lot of time on google:
    1. write something to gui textbox (mission start)
    2. run something lenghty
    3. write something to gui textbox again ( mission ended)

    solution:
    http://www.javafaq.nu/java-example-code-981.html

    the key is:
    1. write to gui
    2. create thread, inside it determine what to do when it is finished
    3. start the thread
    Last edited by BubikolRamios; 01-08-2011 at 11:17 PM.
    Found a flower or bug and don't know what it is ?
    agrozoo.net galery
    if you don't spot search button at once, there is search form:
    agrozoo.net galery search


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •