Results 1 to 5 of 5
  1. #1
    Join Date
    Apr 2010
    Location
    Warakapola, Sri Lanka
    Posts
    15
    Rep Power
    0

    Default 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.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,335
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by darshanaprasad View Post
    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.
    Why not put it in your constructor? You're decorating your Frame there already ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    dlorde is offline Senior Member
    Join Date
    Jun 2008
    Posts
    339
    Rep Power
    7

    Default

    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.

  4. #4
    Join Date
    Apr 2010
    Location
    Warakapola, Sri Lanka
    Posts
    15
    Rep Power
    0

    Default

    Thank you very much.

    Great advices...

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default

    Quote Originally Posted by dlorde View Post
    ... 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.
    From JFrame.java, lines 273-294 (comment added)
    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;
    	     }
            }
        }
    DISPOSE_ON_CLOSE doesn't necessarily terminate the program. If you have a Swing Timer running, for example, it will keep the program alive.

    db

Similar Threads

  1. windowClosed() vs windowClosing()
    By Lil_Aziz1 in forum New To Java
    Replies: 6
    Last Post: 08-05-2010, 04:49 AM
  2. key event help
    By Kyle227 in forum New To Java
    Replies: 6
    Last Post: 05-09-2010, 06:01 AM
  3. checking for an event during an event
    By infinity in forum AWT / Swing
    Replies: 22
    Last Post: 04-09-2009, 01:08 AM
  4. Key event
    By ivvgangadhar in forum Threads and Synchronization
    Replies: 1
    Last Post: 12-11-2008, 09:27 AM
  5. Event
    By nt5515 in forum New To Java
    Replies: 0
    Last Post: 02-15-2008, 04:44 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •