Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7518467
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T01:45:16+00:00 2026-05-30T01:45:16+00:00

I have a simple java GUI application, which will prompt a user for a

  • 0

I have a simple java GUI application, which will prompt a user for a message like “Are you sure you want to quit?”, before he quits the program. Though this only works when I use my Exit Program JButton, but when I use the red cross in the JFrame title bar, then it doesn’t matter if I click either yes or no in the message dialog window.

For this task I have added a new WindowListener to my JFrame, with this code

frame.addWindowListener(new WindowAdapter() {
    public void windowClosing(WindowEvent evt) {
        int Answer = JOptionPane.showConfirmDialog(frame, "You want to quit?", "Quit", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
        if (answer == JOptionPane.YES_OPTION) {
            System.exit(0);
        }
    }
});

If I click no, the program exits anyway, how can I stop that action?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T01:45:17+00:00Added an answer on May 30, 2026 at 1:45 am

    Seems to me that you need this line:

    frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
    

    This means your frame does not close when you click the red X on the upper right. But that also means that you need your own exiting-implementation which you already provided.

    EDIT: And it seems to me that your code won’t work (I couldn’t test it so I am not sure about that). You need the windowClosing() method.

    frame.addWindowListener(new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
             int Answer = JOptionPane.showConfirmDialog(frame, "You want to quit?", "Quit", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
             if (answer == JOptionPane.YES_OPTION)
                 exit(frame);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a windows application which has a complex GUI that I would like
I'm a Java beginner. I already created a simple GUI application that display will
I have a simple GUI component written in Java. The class draws an analog
I have a very simple Java RMI Server that looks like the following: import
I have a GUI application that needs to do something simple in the background
I have a very simple problem. I have an application which is written in
I try to build a gui (Swing) for a simple java application. The application
I have an network application which uses a select loop like this: bool shutdown=false;
I have simple problem as I am not much familiar with Java GUI. I
I am developing desktop GUI application using java swing. And I want to show

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.