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 7708613
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T00:39:21+00:00 2026-06-01T00:39:21+00:00

I have a question about joptionpane. Using JOptionPane.showMessageDialog(…), we can create a message dialog.

  • 0

I have a question about joptionpane.

Using JOptionPane.showMessageDialog(…), we can create a message dialog. But how to close it programmatically?

  • 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-06-01T00:39:22+00:00Added an answer on June 1, 2026 at 12:39 am

    You could always get a reference to the JOptionPane by getting the WindowAncestor of any component it’s holding, and then call dispose() or setVisible(false) on the Window returned. The Window can be obtained by using SwingUtilities.getWindowAncestor(component)

    For example:

    import java.awt.Window;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.*;
    
    public class CloseOptionPane {
    
       @SuppressWarnings("serial")
       private static void createAndShowGui() {
          final JLabel label = new JLabel();
          int timerDelay = 1000;
          new Timer(timerDelay , new ActionListener() {
             int timeLeft = 5;
    
             @Override
             public void actionPerformed(ActionEvent e) {
                if (timeLeft > 0) {
                   label.setText("Closing in " + timeLeft + " seconds");
                   timeLeft--;
                } else {
                   ((Timer)e.getSource()).stop();
                   Window win = SwingUtilities.getWindowAncestor(label);
                   win.setVisible(false);
                }
             }
          }){{setInitialDelay(0);}}.start();
    
          JOptionPane.showMessageDialog(null, label);
    
       }
    
       public static void main(String[] args) {
          SwingUtilities.invokeLater(new Runnable() {
             public void run() {
                createAndShowGui();
             }
          });
       }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a question about using streams in .NET to load files from disk.
I have a question about using os.execvp in Python. I have the following bit
I have a question about using new[] . Imagine this: Object.SomeProperty = new[] {string1,
I have a question about this question . I posted a reply there but
I have this question about umbraco structuring and I can't find the answer anywhere.
I saw a lot of examples of CopyStream implementation but I have question about
Have a question about VBS: How can i start a new window (e.g. of
I have question regarding about finding subview using '.tags' in one UIView. for (UIView
I'm learning asp.net. I have question about example buttons I can use two types
Have a question about full text search. I have a query that works, but

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.