Hai armi,
try this and tell me.... this works fine....
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import javax.swing.JWindow;
import javax.swing.event.EventListenerList;
import oracle.jdeveloper.layout.XYLayout;
import javax.swing.JButton;
import java.awt.Rectangle;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class Class1 extends JFrame implements WindowListener
{
private JPanel jPanel1 = new JPanel();
private XYLayout xYLayout1 = new XYLayout();
private JButton jButton1 = new JButton();
public Class1()
{
try
{
jbInit();
}
catch(Exception e)
{
e.printStackTrace();
}
}
private void jbInit() throws Exception
{
jPanel1.setLayout(null);
jButton1.setText("jButton1");
jButton1.setBounds(new Rectangle(145, 105, 95, 40));
jButton1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
jButton1_actionPerformed(e);
}
});
jPanel1.add(jButton1, null);
this.addWindowListener(this);
this.getContentPane().add(jPanel1, BorderLayout.CENTER);
this.setSize(400,400);
this.setVisible(true);
}
private void jButton1_actionPerformed(ActionEvent e)
{
windowClosing(new WindowEvent(this, WindowEvent.WINDOW_CLOSING));
}
public static void main(String args[])
{
Class1 c = new Class1();
}
public void windowOpened(WindowEvent e)
{
}
public void windowIconified(WindowEvent e)
{
}
public void windowDeiconified(WindowEvent e)
{
}
public void windowDeactivated(WindowEvent e)
{
}
public void windowClosing(WindowEvent e)
{
System.out.println("Hai");
hide();
}
public void windowClosed(WindowEvent e)
{
}
public void windowActivated(WindowEvent e)
{
}
}
Hope this helps.
Regards,
Ciya.
Just for the record, the following code will actually trigger a "real" window closing event programmatically:
JFrame frame = ...
Toolkit tk = Toolkit.getDefaultToolkit();
EventQueue evtQ = tk.getSystemEventQueue();
evtQ.postEvent(new WindowEvent(frame, WindowEvent.WINDOW_CLOSING));
I wouldn't use this myself though.