Results 1 to 6 of 6
  1. #1
    Rsmpt is offline Member
    Join Date
    Apr 2012
    Posts
    30
    Rep Power
    0

    Default "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:
    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) {
                       
                    }
                        }
                }
             };
    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.

    and there is a pic from the bug that should make it more clear:
    "While" (?) bugs don't let me open others forms (frames)-while-bug.jpg

    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.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default 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.

  3. #3
    Rsmpt is offline Member
    Join Date
    Apr 2012
    Posts
    30
    Rep Power
    0

    Default 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...

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: "While" (?) bugs don't let me open others forms (frames)

    Quote Originally Posted by Rsmpt View Post
    A listener works with variable too? I thought they were only for button and stuff like this... Can you please tell me more...
    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.

  5. #5
    Rsmpt is offline Member
    Join Date
    Apr 2012
    Posts
    30
    Rep Power
    0

    Default 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?

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: "While" (?) bugs don't let me open others forms (frames)

    Quote Originally Posted by Rsmpt View Post
    And can i use this system to detect the change of a variable in another form?
    I'm not sure what you mean by "another form", but if you mean in another class, then certainly -- I use it all the time myself as a way to watch the state of my objects while minimizing coupling.

Similar Threads

  1. Replies: 1
    Last Post: 02-01-2012, 08:37 PM
  2. Hwlp with "Open", "Save", "Save as..."
    By trill in forum New To Java
    Replies: 3
    Last Post: 11-02-2010, 09:26 AM
  3. Replies: 2
    Last Post: 01-24-2009, 06:56 PM
  4. Replies: 1
    Last Post: 10-20-2008, 07:35 AM
  5. Open "Save Page As" Dialog Box
    By Anubha in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 12-12-2007, 09:27 PM

Posting Permissions

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