Results 1 to 5 of 5
  1. #1
    ooJavaoo is offline Member
    Join Date
    May 2011
    Posts
    2
    Rep Power
    0

    Default JFrame always closes despite of option chosen

    I'm trying to write a window handler but for some reason, my JFrame always closes when user clicks the 'X' button, even if the user clicks 'No' or 'Cancel' in the confirmation box. Can anyone check the code for me please?

    private class WindowHandler implements WindowListener
    {
    public void windowClosing(WindowEvent arg0)
    {
    if (JOptionPane.showConfirmDialog(null, "Are You Sure?", "Quitting",
    JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE) == JOptionPane.YES_OPTION)
    System.exit(0);
    }

    public void windowOpened(WindowEvent arg0) {
    }

    public void windowClosed(WindowEvent arg0) {
    }

    public void windowIconified(WindowEvent arg0) {
    }

    public void windowDeiconified(WindowEvent arg0) {
    }

    public void windowActivated(WindowEvent arg0) {
    }

    public void windowDeactivated(WindowEvent arg0) {
    }

    }

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,786
    Blog Entries
    7
    Rep Power
    21

    Default

    Add the following line when you initialize your window:

    Java Code:
    setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,457
    Rep Power
    20

    Default

    Extend WindowAdapter instead of implementing WindowListener and you won't need to have a bunch of empty methods.

    db

  4. #4
    ooJavaoo is offline Member
    Join Date
    May 2011
    Posts
    2
    Rep Power
    0

    Default

    Thanks Jos! it worked like a charm! :D

  5. #5
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,786
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by ooJavaoo View Post
    Thanks Jos! it worked like a charm! :D
    Also pay close attention to what Darryl wrote: extend the WindowApapter class so you won't need all those empty methods.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Replies: 9
    Last Post: 05-05-2010, 05:33 PM
  2. how do i count the letter that the user had chosen?
    By ashin in forum Advanced Java
    Replies: 4
    Last Post: 10-08-2009, 06:00 PM
  3. Replies: 2
    Last Post: 03-23-2009, 10:02 AM
  4. [SOLVED] Application should wait until a Dialog closes
    By hannehomuth in forum Advanced Java
    Replies: 2
    Last Post: 07-24-2008, 03:51 PM
  5. Middle Click Closes Tab Eclipse Plugin 1.0.0
    By JavaBean in forum Java Software
    Replies: 0
    Last Post: 10-20-2007, 06:59 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
  •