Results 1 to 6 of 6
- 05-11-2012, 09:35 PM #1
Member
- Join Date
- Apr 2012
- Posts
- 30
- Rep Power
- 0
"While" (?) bugs don't let me open others forms (frames)
Hi Java Forums,
We are the same group from that thread: Can't call variable from other class
Now we have a different problem.
We are making a turn based combat game and there is a form called CombateFrame, this form has the information of the 2 personages that are figthing, as their HP, attack, etc..., and there is a button that when we click it, it will set visible another frame, Attack1.
That frame is correct and the way we set the frame visible too. But when we add a While, in CombateFrame form, for check some status (if hp equals 0, for example) and count the damage that Attack1 frame will send (it is a variable called damage, that copies damage from Attack1 class value), the form doesn't fully charge.
we tried a while and tried to create a thread. There it is:
If we place the r1.run() inside the conctructer of CombateFrame the frame of CombateFrame doesn't load. If we place r1.run() inside the button actionPeformed, inside the CombateFrame class, the form that doesn't load is Ataques1.Java Code:Runnable r1 = new Runnable() { @Override public void run() { while(true){ try { System.out.println("Hello, world!"); Thread.sleep(1000); if (check){ bAttack.setEnabled(true); damagefin = ataques1Frame.dano; check2 = true; } if (check2){ hp1 = hp1 - damagefin; } if (!check){ bAttack.setEnabled(false); System.out.println("2"); } } catch (InterruptedException iex) { } } } };
and there is a pic from the bug that should make it more clear:

I suppose it is maybe from the repeating that doesn't stop, so the program can't do the 2 thing at same time (load form, and keep working the while thread)
Help us, please. How can we do a infinite loop without any error, while others forms work?Last edited by Rsmpt; 05-11-2012 at 09:38 PM.
-
Re: "While" (?) bugs don't let me open others forms (frames)
You're stepping on the Swing event thread or EDT with your while loop, a common error. Doing a long running process in the Swing event thread will prevent it from drawing to the GUI or responding to user events, and in effect fully freezes your application.
Solution -- don't do this. If you need to poll a variable, use a Swing Timer. Better still, don't do any polling at all but instead use listeners to listen for state changes.
- 05-11-2012, 10:14 PM #3
Member
- Join Date
- Apr 2012
- Posts
- 30
- Rep Power
- 0
Re: "While" (?) bugs don't let me open others forms (frames)
A listener works with variable too? I thought they were only for button and stuff like this... Can you please tell me more...
-
Re: "While" (?) bugs don't let me open others forms (frames)
Yep absolutely, you can make the variable a "Bound Property" by using PropertyChangeSupport and PropertyChangeListeners:
Bound Properties
Properties
Other decent tutorials can be found via Google.
- 05-11-2012, 10:38 PM #5
Member
- Join Date
- Apr 2012
- Posts
- 30
- Rep Power
- 0
Re: "While" (?) bugs don't let me open others forms (frames)
And can i use this system to detect the change of a variable in another form?
-
Re: "While" (?) bugs don't let me open others forms (frames)
Similar Threads
-
loop "play again" in an 8 ball game , loops but wont let me answer my "out.print"
By IareSmart in forum New To JavaReplies: 1Last Post: 02-01-2012, 08:37 PM -
Hwlp with "Open", "Save", "Save as..."
By trill in forum New To JavaReplies: 3Last Post: 11-02-2010, 09:26 AM -
MoneyOut.println("It took you (whats wrong?>",year,"<WW?) years to repay the loan")
By soc86 in forum New To JavaReplies: 2Last Post: 01-24-2009, 06:56 PM -
the dollar sign "$", prints like any other normal char in java like "a" or "*" ?
By lse123 in forum New To JavaReplies: 1Last Post: 10-20-2008, 07:35 AM -
Open "Save Page As" Dialog Box
By Anubha in forum JavaServer Pages (JSP) and JSTLReplies: 0Last Post: 12-12-2007, 09:27 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks