on pressing default cancel icon[x] of called JFrame layout class, calling layout class gets cancelled too

StackOverflow https://stackoverflow.com/questions/23592220

  •  20-07-2023
  •  | 
  •  

Domanda

Class1 which is a NewJFrame Form and is also a calling class to class2, NewJFrame3 has a Jbutton component, jButton1, for which method actionPerformed has been over-ridden. I call the object with a thread. On calling, Class2, NewJFrame3, a new frame pops up. Problem is when I press the default cross of called class, NewJFrame3, button [X], both screens gets cancelled. I was not using threads before and was calling the layout method for JFrame3 with just object, but it had this problem, so i used thread but it wouldnt work.

Code of calling class (Layout is fine for this and on clicking its button JButton1, class NewJframe3’s layout pops up) :

public class NewJFrame extends javax.swing.JFrame implements ActionListener
{
public NewJFrame() 
{
    initComponents();
        jButton1.addActionListener(this);
}

private void initComponents() 
{
//layout code for JFrame1 – gives the desired layout/output
}

public void actionPerformed(ActionEvent e) 
    {
    If(some_condition)
        {
            NewJFrame3 obj2 = new NewJFrame3();    
                Thread th1 = new Thread(obj2, "thread1");
                th1.start();
                }
    }
 public static void main(String args[]) 
{
           java.awt.EventQueue.invokeLater(new Runnable() 
                   {
                        public void run() 
                               {
                                    new NewJFrame().setVisible(true);             

                               }
                   });
}//Main ends
}//Calling Class NewJFrame1 ends

Code for called class(same package) :

public class NewJFrame3 extends javax.swing.JFrame implements Runnable
{
               public void run() 
                  {
                       // System.out.println("Inside Run");
                           this.test();
                           this.setVisible(true);
                  }

             protected void test()
                 {
                        //layout code for JFrame3 – gives the desired layout/output
                 }

        PSVM()
          {
//Main method code not relevant here, since layout function is called from run() which gets called on starting thread for this class.
           }

} //Called Class NewJFrame3 end

Thanks a ton for your time!

È stato utile?

Soluzione

  • you have to set for defaultCloseOperation, set to DISPOSE_ON_CLOSE in this case, last of JFrames to terminating an Current JVM

  • your current issue is default value HIDE_ON_CLOSE, then current JVM is still running, consume and increase RAM in PC

  • use CardLayout or JDialog with parent, and/or setModal/ ModalityTypes instead of two JFrames

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top