-1

i want to hide the previous window frame when a new window appear after pressing the submit button,how to hide the previous window or close it without pressing cross button

enter code here
public static void main(String[] args) 
 {

        JFrame frame = new JFrame("Project Format Creator");

        JButton btn5 = new JButton("submit"); 
        JPanel panel = new JPanel(new GridBagLayout());
        GridBagConstraints cst = new GridBagConstraints();

        cst.fill = GridBagConstraints.HORIZONTAL;
        cst.gridx = 0;
        cst.gridwidth = 1; 
        cst.weightx = 0.1;
        cst.gridy = 8;       //third row
        panel.add(btn5,cst);

      btn5.addActionListener(new ActionListener()
      {
         public void actionPerformed(ActionEvent e)
         {
            JFrame frame1 = new JFrame("Project Format Creator");
            frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame1.setSize(300,300);//int width int height
            frame1.getContentPane().add(panel);
            frame1.setVisible(true);

         }
     });



         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         frame.setSize(300,300);//int width int height
         frame.getContentPane().add(panel);
         frame.setVisible(true);
} 
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • You can use `yourFrame.setVisible(false);`. Have a look [here](http://stackoverflow.com/questions/1234912/how-to-programmatically-close-a-jframe) – x80486 Apr 09 '15 at 18:26
  • See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/q/9554636/418556) – Andrew Thompson Apr 10 '15 at 04:04

1 Answers1

1

Use Frame.setVisible(false); if you're going to show that JFrame again, or Frame.dispose(); if you're done with it.

   public void actionPerformed(ActionEvent e)
     {
        frame.dispose(); // dispose the old frame
        JFrame frame1 = new JFrame("Project Format Creator");
        frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame1.setSize(300,300);//int width int height
        frame1.getContentPane().add(panel);
        frame1.setVisible(true);
     }
ControlAltDel
  • 33,923
  • 10
  • 53
  • 80