Results 1 to 5 of 5
Thread: Where to put windowClosing Event
- 05-23-2011, 03:55 PM #1
Member
- Join Date
- Apr 2010
- Location
- Warakapola, Sri Lanka
- Posts
- 15
- Rep Power
- 0
Where to put windowClosing Event
Hi Friends,
I need to add window Closing Event to following code to close the program when clicked close button.
<code>
import java.awt.*;
import java.awt.event.*;
public class App1 extends Frame implements ItemListener
{
private Choice choice1;
Label label1 = new Label("Make a Choice!");
App1()
{
super("Sample Choice");
add(label1, BorderLayout.NORTH);
choice1 = new Choice();
choice1.addItem("One");
choice1.addItem("Two");
choice1.addItem("Three");
choice1.addItemListener(this);
add(choice1, BorderLayout.CENTER);
pack();
setSize(200, getPreferredSize().height);
setLocation(25, 25);
setVisible(true);
}
public void itemStateChanged(ItemEvent ie)
{
String state = "Deselected";
if(ie.getStateChange() == ItemEvent.SELECTED)
{
state = "Selected";
}
label1.setText(ie.getItem() + " " +state);
}
public static void main(String args[])
{
new App1();
}
}
</code>
I need to insert the code,
<code>
addWindowListener(new WindowAdapter(){
public void WindowClosing(WindowEvent e) {System.exit(0);}});
</code>
into the above program.
Please help me to add this code into my program.
- 05-23-2011, 04:26 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
- 05-23-2011, 04:33 PM #3
Senior Member
- Join Date
- Jun 2008
- Posts
- 339
- Rep Power
- 5
Put it in the app constructor.
Having said that, calling System.exit() directly is nearly always a poor practice, and calling it from a window event handler is probably asking for trouble. If you want your application to exit when the frame is closed, set the frame's default close operation to DISPOSE_ON_CLOSE or EXIT_ON_CLOSE.
If you must use a WindowAdapter, don't forget that the method is windowClosing(..) not WindowClosing(..).Last edited by dlorde; 05-23-2011 at 04:35 PM.
- 05-23-2011, 04:36 PM #4
Member
- Join Date
- Apr 2010
- Location
- Warakapola, Sri Lanka
- Posts
- 15
- Rep Power
- 0
Thank you very much.
Great advices...
- 05-23-2011, 06:46 PM #5
From JFrame.java, lines 273-294 (comment added)
DISPOSE_ON_CLOSE doesn't necessarily terminate the program. If you have a Swing Timer running, for example, it will keep the program alive.Java Code:protected void processWindowEvent(WindowEvent e) { super.processWindowEvent(e); if (e.getID() == WindowEvent.WINDOW_CLOSING) { switch(defaultCloseOperation) { : : case EXIT_ON_CLOSE: System.exit(0); // <---- oops! break; } } }
db
Similar Threads
-
windowClosed() vs windowClosing()
By Lil_Aziz1 in forum New To JavaReplies: 6Last Post: 08-05-2010, 04:49 AM -
key event help
By Kyle227 in forum New To JavaReplies: 6Last Post: 05-09-2010, 06:01 AM -
checking for an event during an event
By infinity in forum AWT / SwingReplies: 22Last Post: 04-09-2009, 01:08 AM -
Key event
By ivvgangadhar in forum Threads and SynchronizationReplies: 1Last Post: 12-11-2008, 09:27 AM -
Event
By nt5515 in forum New To JavaReplies: 0Last Post: 02-15-2008, 04:44 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks