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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T18:47:56+00:00 2026-05-22T18:47:56+00:00

I am creating a popup JFrame that will have a message and yes/no buttons.

  • 0

I am creating a popup JFrame that will have a message and yes/no buttons. I am using this method in 2 ways. In 1, the main program calls this method and in the other, this method is called directly after a previous JFrame is closed. This method works when being called form the main program, but when another JFrame calls it, the JFrame created in this method shows up completely blank and the GUI freezes. I cannot exit out of the JFrame, but I can move it. The freezing is a result of the Thread.yield because response is always null, but in what instances will the JFrame fail to be created properly?

Note: response is a static variable. Also when this JFrame is created by another JFrame, the original JFrame does not exit correctly. That JFrame has a JComboBox, and the selected option is frozen on the dropdown. When it does not call this method, it closes properly.

public static String confirmPropertyPurchase(String message)
    {
        response = null;
        final JFrame confirmFrame = new JFrame("Confirm");
        confirmFrame.addWindowListener(new WindowAdapter(){
            public void windowClosing(WindowEvent ev){
                response = "No";
            }
            public void windowDeactivated(WindowEvent e) {
                confirmFrame.requestFocus();
            }
        });

        final JPanel confirmPanel = new JPanel();
        final JButton yes = new JButton();
        final JButton no = new JButton();
        yes.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent arg0){
                response = "Yes";
                confirmFrame.setVisible(false);
            }
        });
        no.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent arg0){
                response = "No";
                confirmFrame.setVisible(false);
            }
        });

        final JLabel confirmLabel = new JLabel("      " + message);
        yes.setText("Yes");
        yes.setPreferredSize(new Dimension(100, 100));
        no.setText("No");
        no.setPreferredSize(new Dimension(100,100));
        confirmFrame.add(confirmLabel, BorderLayout.CENTER);
        confirmPanel.add(yes);
        confirmPanel.add(no);
        confirmFrame.add(confirmPanel, BorderLayout.AFTER_LAST_LINE);
        confirmFrame.setPreferredSize(new Dimension(520, 175
        ));

        confirmFrame.pack();
        confirmFrame.setVisible(true);

        while(response == null)
        {
            Thread.yield();
        }
        return response;
    }
  • 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-22T18:47:56+00:00Added an answer on May 22, 2026 at 6:47 pm

    Again, you shouldn’t be using a JFrame as a dialog. In fact your whole bit of code can be replaced with a simple JOptionPane. e.g.,

      Component parent = null;  // non-null if being called by a GUI
      queryString = "Do you want fries with that?";
      int intResponse = JOptionPane.showConfirmDialog(parent, queryString,
               "Confirm", JOptionPane.YES_NO_OPTION);
      myResponse = (intResponse == JOptionPane.YES_OPTION) ? "Yes" : "No";
      System.out.println(myResponse);
    

    And this:

        while(response == null)
        {
            Thread.yield();
        }
    

    should never be called on the main Swing thread, the EDT or event dispatch thread. The reason the code works when it does is because you’re calling this little bit above off of the EDT, but when you call it on the EDT it freezes the EDT and thus the entire GUI. Simply don’t do it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What do people recommend for creating popup's in ASP.Net MVC? I have used the
While creating a file synchronization program in C# I tried to make a method
I have 3 objects that extend JFrame let's call them FrameA FrameB FrameC .
I have a UI built inside a JFrame. One of the buttons starts a
I read this article regarding creating popup notes with javascript and css The problem
I'm creating a popup window with no URL source using window.open(). I don't give
I'm creating a popup window that has a beforeunload handler installed. When the Close
Creating Traversals for Binary Search Tree with Recursion. void inOrder(void (*inOrderPtr)(T&)) { if(this->left !=
When creating a web application, and lets say you have a User object denoting
While creating classes in Java I often find myself creating instance-level collections that I

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.