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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T02:47:30+00:00 2026-05-28T02:47:30+00:00

When I learned creating Java GUI:s in my first Java course, I was taught

  • 0

When I learned creating Java GUI:s in my first Java course, I was taught to create my windows as JFrame instances, and then add a JPanel to each JFrame and finally add all the GUI components to the JPanel:

class Example extends JFrame {
    Example() {
        JPanel panel = new JPanel();
        this.add(panel);

        // Create components here and add them to panel
        // Perhaps also change the layoutmanager of panel

        this.pack();
        this.setVisibility(true);
    }

    public static void main(String[] args) {
        new Example();
    }
}

I always though “well, this smells a little; I don’t like creating an extra object just to be a container,” but I didn’t know any other way to do it so I just went on with it. Until recently, when I stumbled over this “pattern”:

class AnotherExample extends JFrame {
    AnotherExample() {
        Container pane = this.getContentPane();

        // Add components to and change layout of pane instead

        this.pack();
        this.setVisibility(true);
    }

    public static void main(String[] args) {
        new AnotherExample();
    }
}

Still being quite new to Java, I feel better about the second approach just because it doesn’t involve creating a JPanel just to wrap the other components. But what are the real differences between the approaches, except from that? Does any one of them have any great benefits over the other?

  • 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-28T02:47:31+00:00Added an answer on May 28, 2026 at 2:47 am

    I prefer to create a JPanel (which, being a Swing container, can have a border) and set it as the content pane.

    To get a JComponent out of the content pane requires casting, which has an even worse smell than creating an extra component.

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

Sidebar

Related Questions

I learned Java in college, and then I was hired by a C# shop
I learned today that NetBeans 6.5 should have an on-the-fly compilation of (single) Java
I first learned about Data, context, and interaction (DCI) through this blog post .
I've been charged with creating a little GUI launcher app to go on a
I just learned the difference between the stack and the heap. After creating a
I'm creating a simple GUI app using Tkinter with Python, but I'm having problems
I come from a Java background and recently decided to try creating a C++
In school I have learned that creating UML Diagrams/Models is great design practice before
I learned how to create a custom Qt Widget. I have two manuals for
So I'm learning java. I'm one month in and I just learned about constructors.

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.