Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 05-12-2012, 09:20 PM   PM User | #1
PStigerID
New to the CF scene

 
Join Date: May 2011
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
PStigerID is an unknown quantity at this point
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
PStigerID is offline   Reply With Quote
Old 05-13-2012, 06:15 PM   PM User | #2
alykins
Senior Coder

 
alykins's Avatar
 
Join Date: Apr 2011
Posts: 1,608
Thanks: 37
Thanked 183 Times in 182 Posts
alykins will become famous soon enough
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
alykins is offline   Reply With Quote
Old 05-14-2012, 01:06 PM   PM User | #3
PStigerID
New to the CF scene

 
Join Date: May 2011
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
PStigerID is an unknown quantity at this point
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
PStigerID is offline   Reply With Quote
Old 05-14-2012, 03:37 PM   PM User | #4
alykins
Senior Coder

 
alykins's Avatar
 
Join Date: Apr 2011
Posts: 1,608
Thanks: 37
Thanked 183 Times in 182 Posts
alykins will become famous soon enough
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
alykins is offline   Reply With Quote
Reply

Bookmarks

Tags
background, jtextfield, loop, settext

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:12 PM.


Advertisement
Log in to turn off these ads.