close application from a button on a panel

Hi,

I have created a JFrame which implements this:

this.addWindowListener(new WindowAdapter(){

publicvoid windowClosing(WindowEvent e)

{//do various things in here...

}

});

On this frame there is a panel. On the panel there is a button that it is supposed to close the application. How can I make it close the application so as windowClosing() to be called?

System.exit(0) does not do that.

[680 byte] By [xpantaa] at [2007-11-27 10:59:54]
# 1

System.exit kills the VM dead it in it's tracks.

Try frame.dispose().

If that fails move the code in windowClosing to a method in your frame and just call that method from both windowClosing and the actionPerformed method of your button.

Something likepublic class MainFrame extends JFrame {

private JButton button;

...

public MainFrame() {

this.addWindowListener(new WindowAdapter() {

public void windowClosing(WindowEvent e) {

beforeExiting();

}

});

button = new JButton("Exit");

button.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

beforeExiting();

}

});

...

}

...

private void beforeExiting() {

// do various things in here...

}

}

dwga at 2007-7-29 12:26:14 > top of Java-index,Desktop,Core GUI APIs...
# 2

((JFrame)getParent()).dispose() does not "make a call to windowClosing()", so I decided to create a "saveAndExit()" method that is being called by windowClosing() and by the Panel' button.

I am sorry, I can't. The reason is that the Panel is a custom panel (it extends JPanel) and it belongs to another package. While my main frame belongs to the default package (root folder) my custom panel belongs to another package deeper in the folder hierarchy...

any more ideas?

xpantaa at 2007-7-29 12:26:14 > top of Java-index,Desktop,Core GUI APIs...
# 3

The only thing that I've found somewhere in this forum is the following:

EventQueue evtQ = ((JFrame)getRootPane().getParent()).getToolkit().getDefaultToolkit().getSystemEventQueue();

evtQ.postEvent(new WindowEvent(((JFrame)getRootPane().getParent()), WindowEvent.WINDOW_CLOSING));

Although it works, it is not a good programming practice.

xpantaa at 2007-7-29 12:26:14 > top of Java-index,Desktop,Core GUI APIs...
# 4

works OK like this

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

class Testing extends JFrame

{

public void buildGUI()

{

JButton btn = new JButton("Exit");

getContentPane().add(btn,BorderLayout.SOUTH);

setSize(100,100);

setLocationRelativeTo(null);

setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);

setVisible(true);

addWindowListener(new WindowAdapter(){

public void windowClosing(WindowEvent e){

if (JOptionPane.showConfirmDialog(null,"Exit ?","",JOptionPane.YES_NO_OPTION,-1) == JOptionPane.YES_OPTION)

{

System.exit(0);

}

}

});

btn.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent ae){

WindowEvent close = new WindowEvent(Testing.this,WindowEvent.WINDOW_CLOSING);

processWindowEvent(close);

}

});

}

public static void main(String[] args)

{

SwingUtilities.invokeLater(new Runnable(){

public void run(){

new Testing().buildGUI();

}

});

}

}

Michael_Dunna at 2007-7-29 12:26:14 > top of Java-index,Desktop,Core GUI APIs...