This Java tip illustrates a method of hiding an application frame when its frame is closed. In Java, when the closed button is clicked there is no action defined by default. Generally developer would like to end the application when the frame is closed but if there is a need to reuse the frame for later use the frame can be hidden rather than disposing it.

  // Application frame
    Frame applicationframe = new Frame();
    // Add a listener for the close event
    frame.addWindowListener(new WindowAdapter() {
        public void windowClosing(WindowEvent evt) {
            Frame applicationframe = (Frame)evt.getSource();
            // Hide the frame by setting its visibility as false
            // Further If the frame is no longer needed, call dispose

Parent Category: Java SE Tips