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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T14:40:32+00:00 2026-06-01T14:40:32+00:00

This is a weird problem. I have a solution for it, but I don’t

  • 0

This is a weird problem. I have a solution for it, but I don’t know WHY the problem occurs in the first place. Observe the code below:

// VERSION 1

public class test {

    public static void main(String[] args) {
        JFrame mainFrame = new JFrame("Test");
        JPanel inputPanel = new JPanel();

        mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        mainFrame.getContentPane().add(BorderLayout.CENTER, inputPanel);
        mainFrame.setBounds(100, 50, 200, 100);
        mainFrame.setVisible(true);

        JButton inputFileButton = new JButton("BROWSE");
        inputPanel.add(inputFileButton);
    }
}

It works as expected. The button does nothing, but it renders correctly. Now, I add a JFileChooser (which I plan to do something with later, but for now all I’m doing is instantiating it).

// VERSION 2

public class test {

    public static void main(String[] args) {
        JFrame mainFrame = new JFrame("Test");
        JPanel inputPanel = new JPanel();

        mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        mainFrame.getContentPane().add(BorderLayout.CENTER, inputPanel);
        mainFrame.setBounds(100, 50, 200, 100);
        mainFrame.setVisible(true);

        JFileChooser inputFileChooser = new JFileChooser(); // NEW LINE

        JButton inputFileButton = new JButton("BROWSE");
        inputPanel.add(inputFileButton);
    }
}

All of a sudden my button no longer renders. Why? I know two ways to make it work again, but neither makes 100% sense to me. One way to fix it:

// VERSION 3

public class test {

    public static void main(String[] args) {
        JFrame mainFrame = new JFrame("Test");
        JPanel inputPanel = new JPanel();

        mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        mainFrame.getContentPane().add(BorderLayout.CENTER, inputPanel);
        mainFrame.setBounds(100, 50, 200, 100);
        mainFrame.setVisible(true);

        JButton inputFileButton = new JButton("BROWSE");
        inputPanel.add(inputFileButton);

        JFileChooser inputFileChooser = new JFileChooser(); // MOVE LINE TO END
    }
}

So moving that line to the end allows the button to render again, but that still makes no sense to me what an instantiated JFileChooser has to do with the unconnected button. Another way I can fix this issue:

// VERSION 4

public class test {

    public static void main(String[] args) {
        JFrame mainFrame = new JFrame("Test");
        JPanel inputPanel = new JPanel();

        mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        mainFrame.getContentPane().add(BorderLayout.CENTER, inputPanel);
        mainFrame.setBounds(100, 50, 200, 100);

        JFileChooser inputFileChooser = new JFileChooser();

        JButton inputFileButton = new JButton("BROWSE");
        inputPanel.add(inputFileButton);

        mainFrame.setVisible(true); // MOVE *THIS* LINE TO THE END            
    }
}

It kind of makes sense why the version above fixes the problem… obviously something about the JFileChoose instantiation was making my button invisible, but this setVisible() method afterwards bring it back into the light. But that still doesn’t tell me WHY it went invisible in the first place.

Can somebody please help me figure out what I’m missing? Thanks!

  • 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-01T14:40:33+00:00Added an answer on June 1, 2026 at 2:40 pm

    You are making your mainFrame visible and adding the button afterwards. Take a look at this SO question on what steps you need to take to make sure your button is visible.

    The reason why it works in your first example is probably pure luck. Your call to add the button will be performed before the EDT shows your component.

    Note: please perform the Swing operations on the EDT

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

Sidebar

Related Questions

I have a weird problem, and I don't know if this is the default
I have a really weird problem. The below code works fine if I create
I already have a solution for this problem but I do not understand why
I have this weird problem with setting up cookies with PHP. Everything worked fine
This is a very weird problem. My app that runs just fine but somehow
I have a weird problem while building VS2010 solution incl. unit tests on a
All right, this one is going to sound very weird and I don't know
I have a very weird problem with resx based localization. At first, let me
I have a weird problem I can't solve and can't find any solution to
Good day, I have weird problem with drawing image. Code and pictures say more

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.