Hide Frame when closed example

Submitted by:Andery Smith

Date added:06 November, 2013

Category:Java

This Java code example on how to hide a frame when closed

Tags: hide frame , java frame

Code Snippet:

package com.javacodegeeks.snippets.desktop;
import java.awt.Frame;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class FrameHideEventListener {
public static void main(String[] args) {

// Create the frame
Frame frame = new Frame();

// Add a listener for the close event
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent evt) {
Frame frame = (Frame)evt.getSource();

// Hide the frame
frame.setVisible(false);

// If the frame is no longer needed, call dispose
frame.dispose();
}
});

// Display the frame
int frameWidth = 300;
int frameHeight = 300;
frame.setSize(frameWidth, frameHeight);
frame.setVisible(true);
}
}
 
 

Comments