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

  • Home
  • SEARCH
  • 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 8630441
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T09:03:58+00:00 2026-06-12T09:03:58+00:00

I have a simple Swing GUI. (and not only this, all swing GUI I

  • 0

I have a simple Swing GUI. (and not only this, all swing GUI I have written). When run it, it doesn’t show anything except blank screen, until I resize the main frame, so every components have painted again, and I can show them.

Here is my simple code :

public static void main(String[] args) {
        JFrame frame = new JFrame("JScroll Pane Test");
        frame.setVisible(true);
        frame.setSize(new Dimension(800, 600));

        JTextArea txtNotes = new JTextArea();
        txtNotes.setText("Hello World");
        JScrollPane scrollPane = new JScrollPane(txtNotes);
        frame.add(scrollPane);
}

So, my question is : how can when I start this class, the frame will appear all components I have added, not until I resize frame.

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-12T09:03:59+00:00Added an answer on June 12, 2026 at 9:03 am
    • Do not add components to JFrame after the JFrame is visible (setVisible(true))

    • Not really good practice to call setSize() on frame rather call pack() (Causes JFrame to be sized to fit the preferred size and layouts of its subcomponents) and let LayoutManager handle the size.

    • Use EDT (Event-Dispatch-Thread)

    • call JFrame#setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) as said by @Gilbert Le Blanc (+1 to him) or else your EDT/Initial thread will remain active even after JFrame has been closed

    Like so:

    public static void main(String[] args) {
            //Create GUI on EDT Thread
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
    
                      JFrame frame = new JFrame("JScroll Pane Test");
                      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
    
                      JTextArea txtNotes = new JTextArea();
                      txtNotes.setText("Hello World");
                      JScrollPane scrollPane = new JScrollPane(txtNotes);
                      frame.add(scrollPane);//add components
    
                      frame.pack();
                      frame.setVisible(true);//show (after adding components)
                }
            });
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Recently I have written a pair of applications with JAVA/Swing with a simple GUI:
I have a very simple Swing GUI with just a JTetxtArea. I am trying
i have a desktop GUI in swing , i want to show status of
I have a GUI written using netbeans with a few simple components in place.
I have written a simple server - client program with Swing interface using singleton
I created a simple GUI Application with Swing in Scala, and the Run as
i have a simple swing gui where there are two text fields one for
I have a simple Java Swing GUI Form with a browse button. The browse
I have a simple Swing app compiled with netbeans 7.1.2 on a ubuntu machine
I have a simple Swing Java application that performs searches, and the results are

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.