Header Ads Widget

Responsive Advertisement

Ticker

6/recent/ticker-posts

JFrame hide when click close button (Java)


**********************************************************
COMPLETE SOURCE CODE FOR : HidingJFrameWithoutDestroy.java
**********************************************************
import javax.swing.JFrame;
import javax.swing.JOptionPane;

import java.awt.event.WindowListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class HidingJFrameWithoutDestroy
{
 public static void main(String[]args)
 {
  JFrame a=new JFrame("HIDE A JFRAME");
 
  a.addWindowListener(new WindowAdapter()
  {
   public void windowClosing(WindowEvent evt)
   {
    try
    {
    JFrame temp=(JFrame)evt.getSource();
    temp.setVisible(false);
    String a=JOptionPane.showInputDialog(null,"CHOOSE ONE : \n 1-Make It Visible \n 2-Exit");
    int b=Integer.parseInt(a);
   
    if(b==1)
    {
     temp.show(true);
    }
   
    else if(b==2)
    {
     System.exit(0);
    }
   
    else
    {
     JOptionPane.showMessageDialog(null,"YOU PUT WRONG NUMBER");
    }
    }
    catch(Exception exception)
    {
     JOptionPane.showMessageDialog(null,"ERROR, DON'T DO THAT\nTHIS PROGRAM WILL CLOSE");
     System.exit(0);
    }
   }
  
   public void windowActivated(WindowEvent event)
   {
    JFrame temp=(JFrame)event.getSource();
    temp.setVisible(true);
   }
  }
  );
 
  a.setSize(200,200);
  a.setVisible(true);
 }
}


**********************************************************
JUST COMPILE AND EXECUTE IT
**********************************************************