File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Soft Skills: The software developer's life manual this week in the Jobs Discussion forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

While loop never stops

 
Florin Florentin
Ranch Hand
Posts: 43
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi.

I have a problem. I have a jFrameForm and a GUIController class wich controlls more frames, but the first is that jFrameForm.

jFrameCode:



then i have in the GUIController class


the problem is the loop never stops, it never reach to the line with System.out.println(ag.getOk()); and i don't know why, it never change the value when i press the button. I also had put into while a print statement to verify what value get ok while(ag.getOk()!=1){ System.out.println(ag.getOk()) } and it is always 0.
 
Campbell Ritchie
Sheriff
Pie
Posts: 47216
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch

Your code was difficult to read; I have added CODE tags and you can see how much easier it is to read.
You are returning 0 as default from OK (By the way: that is not at all good style: 0 = false and 1 = true have been [virtually] expunged from Java), and never setting OK to anything else and not doing anything in the loop body . . .

So you will get an infinite loop. Why have you got that loop in the first place?
 
Florin Florentin
Ranch Hand
Posts: 43
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, by default the value is 0 but when i click the button, the ok will be set to 1, so normally the while should stop. Am i not right?

I used the loop because i have to wait untill the button is pressed. When ok is 1 the button is pressed.
 
Brian Legg
Ranch Hand
Posts: 488
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you put any print statements into setOk() to see if the method is even being called when you press the button?
 
Florin Florentin
Ranch Hand
Posts: 43
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes... i did that. The method is called, ok is set to 1 but still the loop don't stop.

Also if i do a print for ok in the while loop i will have only
0
0
0
0
0
0
0
0
0
, even if i called the method setOk.
 
jittu goud
Ranch Hand
Posts: 46
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
did it compile....can you take the pain of posting of posting the complete implementation....
 
Florin Florentin
Ranch Hand
Posts: 43
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



 
Florin Florentin
Ranch Hand
Posts: 43
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i translated the code into English.... where you see Angajat that means Employee .

 
Brian Legg
Ranch Hand
Posts: 488
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't have a compiler or JVM here at work and I am still reading through your code but so far I noticed you are missing the close bracket for your initComponents method and the close bracket for your AngajatGUI class. I'll let you know what else I find. I have a suspicion it is due to the "this" command. You are most likely creating 2 seperate AngajatGUI objects and are getting the wrong "ok" value, or something like that.

...still looking...
 
Brian Legg
Ranch Hand
Posts: 488
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Found it. Your GUIController class is creating a new EmployeeGUI object which has it's own "ok" field. You are also creating a second one in your main method.

I would change your main method to have this in it:



Then inside GUIController I would still create the EmployeeGUI eg and then just set it to visible and keep the rest of the code the same. I don't have a JVM to test it but that should do what you'd like. There is a bit of a logic mixup in your code. Also, you never use jButton2 ;)

Hope that helps.
 
Florin Florentin
Ranch Hand
Posts: 43
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Trank you Brian. I understood what you said and i did some changes in my code.

I commented the new new AngajatGUI().setVisible(true); , and i added eg.setVisible(true); in My GUIController.
Now it works as i wanted. thank you again.

 
Brian Legg
Ranch Hand
Posts: 488
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No problem, glad it worked out.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic