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 4 of 4
  1. #1
    New to the CF scene
    Join Date
    May 2011
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy For loop running in background (jTextField dont change)

    Dear Forum,

    I want to implement a little changing animation, so that each 300 millisecond the jtextField change to the random number (a,b,c,d,e). So I tried this:

    Code:
            for(int f=1;f<40;f++)
            {
                int a = 1 + (int) (Math.random() * 6);
                int b = 1 + (int) (Math.random() * 6);
                int c = 1 + (int) (Math.random() * 6);
                int d = 1 + (int) (Math.random() * 6);
                int e = 1 + (int) (Math.random() * 6);
                jTextField1.setText(a
                    + "");
    
                jTextField2.setText(b
                    + "");
    
                jTextField3.setText(c
                    + "");
    
                jTextField4.setText(d
                    + "");
    
                jTextField5.setText(e
                + "");
                try {
                    Thread.sleep(500);
                } catch (InterruptedException ex) {
                    Logger.getLogger(NewJFrame.class.getName()).log(Level.SEVERE, null, ex);
                }
                System.out.println("done");
    
            }
    In other words: I want to change the input of jTextField multiple times. After I added the println function, I saw that it runs in the background, but the input doesn't change till f = 40.

    This, by the way, is running when a button is pressed.

    I am using NetBeans, Windows 7 64-Bit with a very quick processor (it cant be that it doesn't have enough power not to change it quick enough.

    I am a beginner, please bare this in mind...

    Thanks

    PStigerID

  • #2
    Senior Coder alykins's Avatar
    Join Date
    Apr 2011
    Posts
    1,662
    Thanks
    41
    Thanked 189 Times in 188 Posts
    I think your problem is that the system does not re-draw the screen until the loop finishes... to see this change it so instead of making the labels be set to the number, have it append the number plus a space, you should see a long string of numbers- you need to redraw the screen in order to see it.

    I code C hash-tag .Net
    Reference: W3C W3CWiki .Net Lib
    Validate: html CSS
    Debug: Chrome FireFox IE

  • #3
    New to the CF scene
    Join Date
    May 2011
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by alykins View Post
    I think your problem is that the system does not re-draw the screen until the loop finishes... you need to redraw the screen in order to see it.
    Hi Alykins,

    How can I do this?? Sorry for this dumb question...

    PStigerID

  • #4
    Senior Coder alykins's Avatar
    Join Date
    Apr 2011
    Posts
    1,662
    Thanks
    41
    Thanked 189 Times in 188 Posts
    I am only slowly but surely stepping into the waters of Java; my insight comes bc I do C# and it is very similar... I ran your code through C# windows form and it worked fine...
    I first did it without the loop, just a straight shot to see if it would change- no problem

    then I ran it through with the loop (much smaller though, 10 instead of 40) and after the time period elapsed that it 'would have' finished, suddenly all my labels appeared with the numbers in random order, 10 long (I appended each label to be label1.Text += a + " " so it kept all the numbers... That is what qued me to that idea- essentially the form is not redrawn until that loop finishes and that's why it all came back at the end...

    I also do not do a lot of forms... I use them for really easy quick 'demo' snips- but mainly I work on back end server code... ie all the processing that happens after a form is submitted, running through system process etc... so my xp in drawing to the canvas is null. you'll need to look it up. I presume if you make that all a function instead of a button call, and then have the button call throw threads, you could have threads redrawing while the numbers are changing... warning though- threads are tricky, and can be tough to figure out.

    I code C hash-tag .Net
    Reference: W3C W3CWiki .Net Lib
    Validate: html CSS
    Debug: Chrome FireFox IE


  •  

    Tags for this Thread

    Posting Permissions

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