Here is a piece of code that I wrote but the close button on the top of the application doesn't work please help
Code:
import java.awt.*;
import java.awt.event.*;
public class App extends Frame implements MouseMotionListener {
App() {
addMouseMotionListener(this);
setSize(200, 200);
setVisible(true);
}
public void mouseDragged(MouseEvent e) {
Graphics g = getGraphics();
g.setColor(Color.RED);
g.fillRect(e.getX(), e.getY(),10, 10);
}
public void mouseMoved(MouseEvent e) {
}
public static void main(String[] args)throws Exception {
App a = new App();
}
}
Image:
You need to add Listener and call dispose while initializing your applet.
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
dispose();
}
});
You need to add above line of code inside the constructor.
Try it with that little code:
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent evt) {
System.exit(0);
}
});
You have to insert it in "App()". It will close the program "System.exit(0);"
when you press the close button.
im not using AWT often but heres my Solution :)
addWindowListener(new WindowAdapter(){
#Override
public void windowClosing(WindowEvent we){
System.exit(0);
}
});
Add a new WindowAdapter to your App Constructor and call system.exit(0) on the Window Close Event
You can use WindowContants on JFrame#setDefaultCloseOperation to achieve the desired action. This allows you to dispose the frame, entirely terminate the application and a few more with a single line in the constructor:
this.setDefaultCloseOperation(WindowContants.DISPOSE_ON_CLOSE);
will dispose the frame containing the app. This is sufficient to terminate the program you presented.
This approach doesn't allow handling any events though, but simply closes the frame.
Related
How do I do action when I'm going to terminates the programme.
For example :
When I'm going to terminates the programme, I want to set
if( the x button is clicked or something )
deleteRow from database;
else
just end.
Is it should be done inside the ui frame ?
Use window listeners for that. windowClosing method is used to execute code when window is closing, windowClosed method can be used if you want to do something after the window has closed.
JFrame window=new JFrame("Window");
window.addWindowListener(new WindowAdapter(){
#Override
public void windowClosed(WindowEvent evt)
{
//Do something after window has closed.
}
public void windowClosing(WindowEvent evt)
{
//Do something when window is closing.
//Useful when you have to access data in window(buttons, textfields etc)
}
});
There are also other window event listeners.Documentation of WindowListener
addWindowListener(new WindowAdapter()
{
#Override
public void windowClosing(WindowEvent e)
{
.....
}
});
In my Swing chat application I have a logout button which is used to logout the user and it works fine. Now I need to logout the user when I close the Swing application window.
I did this in web application when closing browser using JavaScript, but now I need to do this in Swing application.
How can I achieve this?
Call JFrame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE)
Add a WindowListener to the frame.
Override the appropriate method of the listener to call your closing method, then set the frame invisible and dispose of it.
E.G.
import java.awt.*;
import java.awt.event.*;
import java.net.URI;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
class CheckExit {
public static void doSomething() {
try {
// do something irritating..
URI uri = new URI(
"http://stackoverflow.com/users/418556/andrew-thompson");
Desktop.getDesktop().browse(uri);
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws Exception {
Runnable r = new Runnable() {
#Override
public void run() {
// the GUI as seen by the user (without frame)
JPanel gui = new JPanel(new BorderLayout());
gui.setPreferredSize(new Dimension(400, 100));
gui.setBackground(Color.WHITE);
final JFrame f = new JFrame("Demo");
f.setLocationByPlatform(true);
f.add(gui);
// Tell the frame to 'do nothing'.
f.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
WindowListener listener = new WindowAdapter() {
#Override
public void windowClosing(WindowEvent we) {
int result = JOptionPane.showConfirmDialog(
f, "Close the application");
if (result==JOptionPane.OK_OPTION) {
doSomething();
f.setVisible(false);
f.dispose();
}
}
};
f.addWindowListener(listener);
// ensures the frame is the minimum size it needs to be
// in order display the components within it
f.pack();
// should be done last, to avoid flickering, moving,
// resizing artifacts.
f.setVisible(true);
}
};
// Swing GUIs should be created and updated on the EDT
// http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html
SwingUtilities.invokeLater(r);
}
}
Use the window events on your JFrame, there you have the Methods you might need (windowclosed();) for example. it´s the WindowListener
edit :
you can say
setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
but your Windowlistener still works if you push the X (close button)
there you override the method windowClosing, with this code
public void windowClosing(WindowEvent e) {
int i = JOptionPane.showConfirmDialog(TestFrame.this, "do you really want to close?","test",JOptionPane.YES_NO_OPTION);
if(i == 0) {
System.exit(0);
}
}
this will do the work
I have a class developed with windowbuilderpro that i want to close also from a JButton further than with the standard X button on the window, so here the example of the class :
public class MainWindow {
public JFrame frame;
public MainWindow() {
initialize();
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
public void show() {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
//Show the main Frame
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
When i close the window from the X button the window close correctly and the process terminate.
When i close instead from a JButton that have this listener :
mntmExit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//Close the application main form
frame.setVisible(false);
frame.dispose();
}
});
the frame window close correctly but the process remain alive ... Why ?
As you can see there is an AWT-Shutdown thread that start and terminate continuously, How can i achieve the same behaviour of the X button that close also the application process ?
Notes :
System.exit(0); is not suitable because it terminate the application also if there are another background running thread and i don't want that . The MainWindow class should close and release it's resource, the same behaviour that have closing the application with the X button that close the MainWindow instance but if there are background thread running it doesn't kill they but wait until they finished their work...
Enviroment :
JDK 7
Eclipse 3.7.1
not sure what you really needed, that looks like that you create new JFrame again an again, don't do that, create JFrame once and re-use this Container
frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); // do nothing
frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE); // same as setVisible(false)
then for visibily you can only to call frame.setVisible(true);
for more Confortable is override WindowListener, then you can control some Events
All threads in this code stop when either the x button or the Exit button are activated. Are you getting different behavior?
import java.awt.event.*;
import javax.swing.*;
public class MainWindow {
public JFrame frame;
JButton mntmExit = new JButton("Exit");
public MainWindow() {
frame = new JFrame("Close Me!");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
mntmExit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//Close the application main form
frame.setVisible(false);
frame.dispose();
}
});
frame.add(mntmExit);
frame.pack();
show();
}
public void show() {
//Show the main Frame
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
MainWindow mw = new MainWindow();
mw.show();
}
});
}
}
Just add one line:
System.exit(0);
What code is called when a JFrame is minimized? Is it hooked up to a listener? I just want to know what happens internally when the frame is minimized.
EDIT:
Im actually looking for the code that is called when the frame is minimized. For example, the code for the actual windowListener. Ive been searching through JFrame, Frame, and Window searching for windowIconified but have been unable to find the actual code.
Reason being, when my program runs, it has a small defect with one of the Panels, but when I minimize and maximize the JFrame, the problem goes away. I wanted to see what was going on so that I can apply whatever is going on to my Panel so it paints right.
you can listening by using WindowListener
for example
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import javax.swing.JFrame;
public class WinStateListener implements WindowListener {
static JFrame window = new JFrame("Window State Listener");
public WinStateListener() {
window.setBounds(30, 30, 300, 300);
window.addWindowListener(this);
window.setVisible(true);
}
public static void main(String[] args) {
WinStateListener winStateListener = new WinStateListener();
}
public void windowClosing(WindowEvent e) {
System.out.println("Closing");
window.dispose();
System.exit(0);
}
public void windowOpened(WindowEvent e) {
System.out.println("Opened");
}
public void windowClosed(WindowEvent e) {
System.out.println("Closed");
}
public void windowIconified(WindowEvent e) {
System.out.println("Iconified");
}
public void windowDeiconified(WindowEvent e) {
System.out.println("Deiconified");
}
public void windowActivated(WindowEvent e) {
System.out.println("Activated");
}
public void windowDeactivated(WindowEvent e) {
System.out.println("Deactivated");
}
}
You want to read about WindowListeners and WindowEvents. The event you are talking about is called Iconifying the window. Read more here:
http://download.oracle.com/javase/tutorial/uiswing/events/windowlistener.html
EDIT:
Use revalidate() then repaint() on the JPanel that is acting up.
When minimizing the JFrame application a window event windowIconified is called. If you want to process such window events by your own then either implement WindowListener interface or use WindowAdapter abstract class.
What code is called when a JFrame is minimized?
As noted in How to Make Frames: Specifying Window Decorations, "window decorations are supplied by the native window system." The article goes on to describe some changes you can make to the host platform's default.
Addendum: Reading your update, note that restoring an iconified window repaints it. As #Andrew Thompson points out, you may need to verify that you're building on the event dispatch thread. You may also need to schedule a repaint(). An sscce might clarify things.
I'm making a program with a logger. The logger has its own JFrame.
I'm trying to override the reaction from clicking on the minimize-button of that frame.
I would like the frame to either setVisible(false) or do the defaultCloseOperation (as i set that to hide earlier).
How should I do this?
Thanks in advance
Use a JDialog instead of a JFrame. JDialogs don't have a minimize button.
You can add a WindowListener and add a iconified handler that will react when the window is minimized.
Maybe:
frame.addWindowListener(new WindowAdapter(){
public void windowIconified(WindowEvent e){
frame.setVisible(false);
}
});
You can use the WindowStateListener like this
f.addWindowStateListener(new WindowStateListener() {
#Override
public void windowStateChanged(WindowEvent arg0) {
if (arg0.getNewState() == Frame.ICONIFIED) {
// do stuff
}
}
});
Try this:
frame.addWindowListener(new WindowAdapter() {
#Override
public void windowIconified(WindowEvent event)
{
//do your stuff
}
});