0

I am creating a frame using NetBeans IDE, by default the frame is closed. I modified the default behaviour such as EXIT_ON_CLOSE to DO_NOTHING_ON_CLOSE. I want to close manually.

How to close JFrame without using default methods?

Here is my code:

public class FrameClose extends javax.swing.JFrame {
    public FrameClose() {

      initComponents();
    }


    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>                        


    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(FrameClose.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(FrameClose.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(FrameClose.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(FrameClose.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new FrameClose().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify                     
    // End of variables declaration                   
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 4
    See [How to Write Window Listeners](http://docs.oracle.com/javase/tutorial/uiswing/events/windowlistener.html). – Andrew Thompson Apr 15 '14 at 12:14
  • 1
    What do you mean you want to close manually – James Parsons Apr 15 '14 at 12:14
  • 2
    Yep, the question is not very clear at all. What are "default methods" which you want to avoid? What do you mean by "close manually"? Also, since you are learning Swing I strongly suggest that you not use a windows builder utility but instead code by hand using the Swing tutorials as a guide until you learn the library. – Hovercraft Full Of Eels Apr 15 '14 at 12:15
  • See [How to programmatically close a JFrame](http://stackoverflow.com/a/1235994/878469). – predi Apr 15 '14 at 13:21
  • Send `WINDOW_CLOSING`, shown [here](http://stackoverflow.com/a/7457102/230513). – trashgod Apr 15 '14 at 14:01

2 Answers2

0

you can use dispose() method on frame object, for ex:

JFrame frame  = new JFrame();
frame.dispose();
astack
  • 3,837
  • 7
  • 22
  • 21
0

You can try using a WindowListener

addWindowListener(new WindowListener() {
        @Override
        public void windowActivated(WindowEvent arg0) { }
        @Override
        public void windowClosed(WindowEvent arg0) { }
        @Override
        public void windowClosing(WindowEvent arg0) { }
        @Override
        public void windowDeactivated(WindowEvent arg0) { }
        @Override
        public void windowDeiconified(WindowEvent arg0) { }
        @Override
        public void windowIconified(WindowEvent arg0) { }
        @Override
        public void windowOpened(WindowEvent arg0) { }
});

Use this instead of calling setDefaultCloseOperation() and then just use which ever method works best for what you want to do.

Or if you want to close the frame in code use setVisible(false); to hide the Frame or use dispose() to get rid of the entire frame

futurii
  • 54
  • 3