Showing posts with label close. Show all posts
Showing posts with label close. Show all posts

Close JFrame when mouse click release in JFrame

Source code below will create a JFrame. After that, click in the JFrame, hold for a few seconds, and after that release your mouse click. The JFrame that created in this program will be close and this application will terminate.

********************************************************************
COMPLETE SOURCE CODE FOR : CloseJFrameWhenMouseReleased.java
********************************************************************


import javax.swing.JFrame;

import java.awt.event.MouseListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class CloseJFrameWhenMouseReleased
{
public static void main(String[]args)
{
//Create mouse listener that will listen when mouse released in JFrame
MouseListener ml=new MouseAdapter()
{
public void mouseReleased(MouseEvent event)
{
//Put JFrame close code here
System.exit(0);
}
};

//Create JFrame with title ( CLICK ON ME,HOLD FOR A FEW SECOND,RELEASE YOUR CLICK )
JFrame frame=new JFrame("CLICK ON ME,HOLD FOR A FEW SECOND,RELEASE YOUR CLICK");

//Add mouse listener to JFrame
frame.addMouseListener(ml);

//Set default close operation to JFrame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//Set JFrame size to :
//Width : 700 pixels
//Height : 400 pixels
frame.setSize(700,400);

//Make JFrame visible. So we can see it.
frame.setVisible(true);
}
}


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

Close JFrame when cursor exit from JFrame

************************************************************************
COMPLETE SOURCE CODE FOR : CloseJFrameWhenMouseExit.java
************************************************************************


import javax.swing.JFrame;

import java.awt.event.MouseListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class CloseJFrameWhenMouseExit
{
public static void main(String[]args)
{
//Create mouse listener that will listen when cursor exit JFrame
MouseListener ml=new MouseAdapter()
{
public void mouseExited(MouseEvent event)
{
//Put JFrame close code here
System.exit(0);
}
};

//Create JFrame with title ( GET YOUR CURSOR INTO JFRAME, AFTER THAT MOVE IT AWAY )
JFrame frame=new JFrame("GET YOUR CURSOR INTO JFRAME, AFTER THAT MOVE IT AWAY");

//Add mouse listener to JFrame
frame.addMouseListener(ml);

//Set default close operation to JFrame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//Set JFrame size to :
//Width : 700 pixels
//Height : 400 pixels
frame.setSize(700,400);

//Make JFrame visible. So we can see it.
frame.setVisible(true);
}
}


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

Close JFrame using mouse hover

Source code below will show you, how to create JFrame that can close, when you hover your cursor on it.

**********************************************************************
COMPLETE SOURCE CODE FOR : CloseJFrameUsingMouseHover.java
**********************************************************************


import javax.swing.JFrame;

import java.awt.event.MouseListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class CloseJFrameUsingMouseHover
{
public static void main(String[]args)
{
//Create mouse listener that will listen when someone hover on JFrame
MouseListener ml=new MouseAdapter()
{
public void mouseEntered(MouseEvent event)
{
//Put JFrame close code here
System.exit(0);
}
};

//Create JFrame with title ( HOVER ON THE BOX BELOW !!!!!!!!! )
JFrame frame=new JFrame("HOVER ON THE BOX BELOW !!!!!!!!!");

//Add mouse listener to JFrame
frame.addMouseListener(ml);

//Set default close operation to JFrame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//Set JFrame size to :
//Width : 400 pixels
//Height : 400 pixels
frame.setSize(400,400);

//Make JFrame visible. So we can see it.
frame.setVisible(true);
}
}


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

Close JFrame using mouse click

Source code below will show you, how to close JFrame when click on it.

*****************************************************************************
COMPLETE SOURCE CODE FOR : CloseJFrameUsingMouseClick.java
*****************************************************************************


import javax.swing.JFrame;

import java.awt.event.MouseListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class CloseJFrameUsingMouseClick
{
public static void main(String[]args)
{
//Create mouse listener that will listen when someone click on JFrame
MouseListener ml=new MouseAdapter()
{
public void mouseClicked(MouseEvent event)
{
//Put JFrame close code here
System.exit(0);
}
};

//Create JFrame with title ( CLICK IN THE BOX BELOW !!!!!!!!! )
JFrame frame=new JFrame("CLICK IN THE BOX BELOW !!!!!!!!!");

//Add mouse listener to JFrame
frame.addMouseListener(ml);

//Set default close operation to JFrame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//Set JFrame size to :
//Width : 400 pixels
//Height : 400 pixels
frame.setSize(400,400);

//Make JFrame visible. So we can see it.
frame.setVisible(true);
}
}


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

Close JFrame using keyboard

Source code below will show you how to close JFrame using keyboard press. In this source code we will use "Esc" key. So when someone press "Esc" key, and at the same time JFrame focused, the JFrame will close and this program will exit.

**********************************************************************
COMPLETE SOURCE CODE FOR : CloseJFrameUsingKeyboard.java
**********************************************************************


import javax.swing.JFrame;

import java.awt.event.KeyListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

public class CloseJFrameUsingKeyboard
{
public static void main(String[]args)
{
//Create a KeyListener that can listen when someone press Esc key on keyboard
//You can change for what key that you want, by change value at:
//VK_ESCAPE
KeyListener kl=new KeyAdapter()
{
public void keyPressed(KeyEvent evt)
{
//If someone click Esc key, this program will exit
if(evt.getKeyCode()==KeyEvent.VK_ESCAPE)
{
System.exit(0);
}
}
};

//Create a JFrame with title ( CLOSE JFRAME USING KEYBOARD )
JFrame frame=new JFrame("CLOSE JFRAME USING KEYBOARD");

//add Key Listener to JFrame
frame.addKeyListener(kl);

//Set default close operation for JFrame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//Set JFrame size to :
//Width : 400 pixels
//Height : 400 pixels
frame.setSize(400,400);

//Make JFrame visible. So we can see it.
frame.setVisible(true);
}
}


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

Close JWindow using mouse click on it

Source code below will show you how to create a JWindow that can terminate when we click on it.

************************************************************************
COMPLETE SOURCE CODE FOR : ClosingJWindowUsingMouseClick.java
************************************************************************


import javax.swing.JWindow;
import javax.swing.JLabel;

import java.awt.event.MouseListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import java.awt.FlowLayout;

public class ClosingJWindowUsingMouseClick
{
public static void main(String[]args)
{
//Create a JWindow
JWindow window=new JWindow();

//Create a JLabel that we want to add to JWindow
JLabel label=new JLabel("Click me to exit");

//Set layout for JWindow
window.setLayout(new FlowLayout());

//Add label to JWindow
window.add(label);

//Create Mouse Listener that listen when someone click on that JWindow
MouseListener ml=new MouseAdapter()
{
public void mousePressed(MouseEvent evt)
{
//Exit application
System.exit(0);
}
};

//Add Mouse Listener to JWindow
window.addMouseListener(ml);

//Set JWindow size with width=400 pixels and height=400 pixels
window.setSize(400,400);

//Make JWindow visible
window.setVisible(true);
}
}


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

Close JWindow using button

Source code below will show you how to exit a JWindow using a button.

************************************************************************
COMPLETE SOURCE CODE FOR : ClosingAJWindowUsingButton.java
************************************************************************


import javax.swing.JWindow;
import javax.swing.JButton;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.FlowLayout;

public class ClosingAJWindowUsingButton extends JWindow implements ActionListener
{
JButton button=new JButton("Exit");

public ClosingAJWindowUsingButton()
{
button.addActionListener(this);
setLayout(new FlowLayout());
add(button);
setSize(400,400);
setVisible(true);
}

public void actionPerformed(ActionEvent evt)
{
if(evt.getSource()==button)
{
System.exit(0);
}
}

public static void main(String[]args)
{
ClosingAJWindowUsingButton caj=new ClosingAJWindowUsingButton();
}
}


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

How to determine when a JFrame opened or closed


*******************************************************
COMPLETE SOURCE CODE FOR : DetermineWhenAJframeIsOpenOrClose.java
*******************************************************


import javax.swing.JFrame;
import java.awt.Frame;
import javax.swing.JButton;
import javax.swing.JOptionPane;

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


public class DetermineWhenAJframeIsOpenOrClose
{
JFrame frame;

JFrame a=new JFrame("OPEN A JFRAME WITH TITLE'S MONSTER");
JButton b=new JButton("CLICK HERE TO OPEN");

public DetermineWhenAJframeIsOpenOrClose()
{

b.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent evt)
{
frame=new JFrame("MONSTER");
frame.setSize(300,500);
frame.setVisible(true);

frame.addWindowListener(new WindowAdapter()
{
//windowOpened METHOD WILL BE CALLED WHEN A JFRAME IS OPENED
public void windowOpened(WindowEvent evt)
{
JOptionPane.showMessageDialog(null,"MONSTER IS OPEN");
}

//windowClosing METHOD WILL BE CALLED WHEN A JFRAME IS CLOSING
public void windowClosing(WindowEvent evt)
{
Frame temp=(Frame)evt.getSource();
temp.dispose();
JOptionPane.showMessageDialog(null,"MONSTER IS CLOSE");
}

});

}
});

a.add(b);

a.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
a.setSize(500,100);
a.setVisible(true);

}

public static void main(String[]args)
{
DetermineWhenAJframeIsOpenOrClose mainObject=new DetermineWhenAJframeIsOpenOrClose();
}
}


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

Close application when click on JFrame close button


*********************************************************
COMPLETE SOURCE CODE FOR : ExitApplicationUsingJFrameCloseButton.java
*********************************************************


import javax.swing.JFrame;

public class ExitApplicationUsingJFrameCloseButton
{
public static void main(String[]args)
{
JFrame a=new JFrame("EXIT AN APPLICATION USING JFRAME CLOSE BUTTON");

//JFrame.EXIT_ON_CLOSE will make your application close when you hit JFrame close button
a.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
a.setSize(200,200);
a.setVisible(true);
}
}


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

JFrame hide when click close button


**********************************************************
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
**********************************************************

RELAXING NATURE VIDEO