Results 1 to 7 of 7
  1. #1
    nish.singha20 is offline Member
    Join Date
    Nov 2011
    Posts
    5
    Rep Power
    0

    Default Terminating execution of a JFrame without terminating execution of child frames...

    Hi!
    Am developing a Java Swing application. What my program does is that, it continuously executes a method in repeatedly. What I want is that if someone presses a JButton on that frame, the another JFrame should be called, but this JFrame should terminate its execution. Normally what is happening is that termination of Parent JFrame is causing the child JFrame to terminate as well. Here is my code :

    Java Code:
     
    
    
    
    private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    
        
    // Want to call another JFrame here and terminate execution of this JFrame. 
        
    }                                        
    
        public void start_here()
        {
            if(i==9)
                i=-1;
            i++;
            jLabel1.setText(questions[i]);
            jLabel1.setVisible(true);
            jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/"+i+".jpg")));
            jButton1.setVisible(true);
            jButton3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/red.jpg")));
            jLabel3.setVisible(false);
            jLabel4.setVisible(false);
            jLabel5.setVisible(false);
            jButton2.setEnabled(true);
            jButton4.setEnabled(true);
            jButton5.setEnabled(true);
                
            timer = new Timer();
            timer.schedule(new wait(),3000);
            
            
            
        }
        class wait extends TimerTask
        {
            public void run()
            {
                System.out.println("Timer expired!");
                timer.cancel();
                jButton2.setEnabled(false);
                jButton4.setEnabled(false);
                jButton5.setEnabled(false);
                jButton3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/green.jpg")));
                jButton3.revalidate();
                jLabel5.setVisible(true);
                int response=0;
                do
                {
                    jButton3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/green.jpg")));
                    jLabel5.setVisible(true);
                    response=recognize();
                    /*try
                        {
                            Thread.sleep(1000);
                        }
                        catch(Exception e)
                        {
                            System.out.println(e);
                        }*/
                    if(response<0)
                    {
                        jLabel2.setVisible(true);
                        jButton3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/red.jpg")));
                        jLabel5.setVisible(false);
                        try
                        {
                            Thread.sleep(3000);
                        }
                        catch(Exception e)
                        {
                            System.out.println(e);
                        }
                    }
                    jLabel2.setVisible(false);
                }while(response<0);
                
                if(response==i)
                {
                    jLabel3.setText("Correct!!!");
                    jLabel3.setVisible(true);
                    jLabel4.setText("You said : "+words[i]);
                    jLabel4.setVisible(true);
                    jButton3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/red.jpg")));
                    jLabel5.setVisible(false);
                }
                else
                {
                    jLabel3.setText("Incorrect!!! Try Again...");
                    jLabel3.setVisible(true);
                    jLabel4.setText("You said : "+words[response]);
                    jLabel4.setVisible(true);
                    i--;
                    jButton3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/red.jpg")));
                    jLabel5.setVisible(false);
                }
                
                
              
                try
                {
                    Thread.sleep(4000);
                }
                catch(Exception e)
                {}
                //jButton3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/red.jpg")));
                
                start_here();
            }
        }
    Last edited by Norm; 11-29-2011 at 09:00 PM. Reason: added code tags

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,331
    Rep Power
    25

    Default Re: Terminating execution of a JFrame without terminating execution of child frames..

    What do you mean by "termination of Parent JFrame"?
    You can call setVisible(false) for the JFrame class and remove ALL references to that class from your program.
    Any methods in that class that are executing need to exit.

  3. #3
    nish.singha20 is offline Member
    Join Date
    Nov 2011
    Posts
    5
    Rep Power
    0

    Default Re: Terminating execution of a JFrame without terminating execution of child frames..

    Hi! Thanks for the reply. Actually as you can see in the code above, the run() method repeatedly calls start_here() method after certain delay. When i press JButton5 what i want is that, this "loop" of calling start_here() method should terminate and should no longer execute. Simply calling setVisible(false) will only make this frame disappear, but the cycle of calling start_here() method continues.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,331
    Rep Power
    25

    Default Re: Terminating execution of a JFrame without terminating execution of child frames..

    When i press JButton5 what i want is that, this "loop" of calling start_here() method should terminate and should no longer execute.
    That will require a bit of programming. Have a boolean flag that you set in the listener for the button and that is tested in the loop. The loop should exit when the flag is set.

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,202
    Rep Power
    19

    Default Re: Terminating execution of a JFrame without terminating execution of child frames..

    Quote Originally Posted by nish.singha20 View Post
    Hi!
    Am developing a Java Swing application. What my program does is that, it continuously executes a method in repeatedly. What I want is that if someone presses a JButton on that frame, the another JFrame should be called, but this JFrame should terminate its execution. Normally what is happening is that termination of Parent JFrame is causing the child JFrame to terminate as well.
    That's what you get for using a GUI builder without learning to code a Swing GUI. Start learning here: Trail: Creating a GUI With JFC/Swing (The Java™ Tutorials)

    Oh, and two JFrames can't have a parent-child relationship.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,202
    Rep Power
    19

    Default Re: Terminating execution of a JFrame without terminating execution of child frames..

    Also, I'd hazard a guess that what you really need is just one JFrame with a CardLayout.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  7. #7
    nish.singha20 is offline Member
    Join Date
    Nov 2011
    Posts
    5
    Rep Power
    0

    Default Re: Terminating execution of a JFrame without terminating execution of child frames..

    @Norm : Thanks for the help.
    @DB : Thanks for the link... :)

Similar Threads

  1. closing JFrame without terminating program
    By chirag123 in forum AWT / Swing
    Replies: 5
    Last Post: 04-25-2011, 01:10 PM
  2. Replies: 10
    Last Post: 11-29-2010, 02:02 PM
  3. Terminating a Thread
    By Lil_Aziz1 in forum Threads and Synchronization
    Replies: 4
    Last Post: 01-31-2010, 10:30 PM
  4. RMI Server Randomly Terminating
    By danberg in forum Networking
    Replies: 2
    Last Post: 04-26-2009, 03:36 AM
  5. terminating a while loop with a string
    By tkdvipers in forum New To Java
    Replies: 3
    Last Post: 07-09-2007, 11:23 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
  •