Hiding Frame in Java

This section illustrates you how to hide the Java Awt frame. Here, you have seen in the given following example that provides you the complete code of the program.

Hiding Frame in Java

This section illustrates you how to hide the Java Awt frame. Here, you have seen in the given following example that provides you the complete code of the program.

Hiding Frame in Java

Hiding Frame in Java

     

Introduction

This section illustrates you how to hide the Java Awt frame. Here, you have seen in the given following example that provides you the complete code of the program.

Description of  program:

In this program, program you will see that only one method of the frame is important for the purpose. The following program show a frame titled with "Frame Hiding". When you click on the close button of the frame, the frame will not be closed but it will hide certain with holding all frame data as it is.

In this program, program we are handling the event generated by the frame when you click on the close button of the frame. By using the getSource() method of the WindowEvent class we are creating the Frame object and frame hides when you call the setVisible() method by passing a boolean valued argument.

Method's Description:

getSource(): This is the method of EventObject class. Hence, here this method is used for the WindowEvent class so, it can return the Form's object that is done invisible by using the setVisible() method. The getSource() method returns the source of the generated event.

setVisible(false): This is the method of Form class i.e. used for setting the form whether visible or not. This method takes a boolean valued argument which decides the form for visible or not. If you pass the boolean value false then the form will be invisible otherwise if you argument is true then the form object will be visible. By default Java Awt Form is in the invisible state. For showing the form you have to set the form visible by using the setVisible(true) method.

Here is the code of program:

import java.awt.*;
import java.awt.event.*;

public class  FrameHide{
  public static void main(String[] args){
  Frame fa= new Frame("Frame Hiding");
  Panel p =new Panel();
  Label l1=new Label("Welcome roseindia");
  p.add(l1);
  fa.add(p);
  fa.setSize(300,200);
  fa.setVisible(true);
  fa.addWindowListener(new WindowAdapter(){
  public void windowClosing(WindowEvent e){
  Frame frame = (Frame)e.getSource();
  frame.setVisible(false);
 }
  });
 }  
}

Output of this Program:

 

Download this example.