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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T19:21:45+00:00 2026-06-13T19:21:45+00:00

While creating a JFrame and adding some components I noticed that if I create

  • 0

While creating a JFrame and adding some components I noticed that if I create a instance of a JComboBox between setting the JFrame visible and adding a button, the button disappears.

I start of by creating a JFrame:

JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(200, 200);

Then I add set the frame visible and add a JButton:

frame.setVisible(true);
frame.add(new JButton("text"));

It works as expected and displays one big button:

JFrame with a button

BUT, if I create one instance of a JComboBox in between those lines:

frame.setVisible(true);
new JComboBox();
frame.add(new JButton("text"));

And now the buttons is gone..

JFrame without a button

I expect no change at all, since I’m only creating a instance and not assigning it to anything.
Why does the button disappears?

Also, if move new JComboBox(); above frame.setVisible(true);, the button goes visible again.

  • 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-13T19:21:47+00:00Added an answer on June 13, 2026 at 7:21 pm

    Once you display the UI, it should not be modified from any thread except of the EDT. In the first case you had some “luck”, and it worked. In the second case probably the time of creation of the JComboBox was long enough to prevent you from modifying the UI from a thread that is not the EDT.

    What you should do, is invoking that code on the EDT:

    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            JFrame frame = new JFrame();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setSize(200, 200);
            frame.add(new JButton("text"));
            frame.setVisible(true);
        }
    })
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

While creating JavaScript with ASP.NET MVC I noticed several scope warnings and realized that
I ran into some trouble while creating a C-Extension for ruby that got me
While creating a website using Twitter Bootstrap's carousel, it seems that some scripts are
While creating a WCF Rest service, I've noticed that not all the parameters in
While creating classes in Java I often find myself creating instance-level collections that I
I just noticed while creating a RESTful WCF service that the Method parameter on
While creating an online shop application using play-1.2.4 ,I ran into some problems with
I am creating a program that uses JFrame, JPanel, JLabel and all other sorts
While creating and inserting DOM element's, it seems that the functions used for the
While creating the sample, I noticed very strange behavior and due to which 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.