how to trigger the windowclosing event manually

I need to trigger windoclosing event for JFrame manullay.My purpose is to close a frame by clicking a button,when button is pressed windowclosing event of that frame must be triggerdhow to do that
[217 byte] By [armia] at [2007-10-2 11:12:33]
# 1
Hai Armi,In ur button_actionperformed just use,this.hide();this.dispose();Hope this helps u...Regards,Ciya...
ciyaa at 2007-7-13 3:56:46 > top of Java-index,Desktop,Core GUI APIs...
# 2
Thanks Ciya,But i actually want code in the window closing event to executedframe.dispose or frame.hide won't trigger the window closing event
armia at 2007-7-13 3:56:46 > top of Java-index,Desktop,Core GUI APIs...
# 3

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.

ciyaa at 2007-7-13 3:56:46 > top of Java-index,Desktop,Core GUI APIs...
# 4
Or something like this: http://forum.java.sun.com/thread.jspa?forumID=57&threadID=695964
camickra at 2007-7-13 3:56:46 > top of Java-index,Desktop,Core GUI APIs...
# 5

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.

happy_hippoa at 2007-7-13 3:56:46 > top of Java-index,Desktop,Core GUI APIs...
# 6
try this,this.processWindowEvent(new WindowEvent(this,WindowEvent.WINDOW_CLOSING));where this represents your window class.
ranjith_iita at 2007-7-13 3:56:46 > top of Java-index,Desktop,Core GUI APIs...
# 7
Thanx ciya,Your codings worked excellently, and make a note that happy_hipo's coding is very handy to use from any class to another class's frame
armia at 2007-7-13 3:56:46 > top of Java-index,Desktop,Core GUI APIs...