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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T07:06:12+00:00 2026-06-17T07:06:12+00:00

Using Java’s Swing, you’ll often have to wrap elements in others to achieve the

  • 0

Using Java’s Swing, you’ll often have to wrap elements in others to achieve the desired layout. These panel’s elements does however need to be accessable by the super-panel (further called view), upon for example, form-submission.

Consider the following (simplified) example:

class AddUserView extends JPanel {

   private JPanel elementWrapper = new JPanel();
   private JPanel buttonWrapper = new JPanel();

   private JTextField userNameField = new JTextField();
   private JButton submitButton = new JButton("Submit");

   public AddUserView() {

      // .. add elements to the wrappers
      elementWrapper.add(userNameField);
      buttonWrapper.add(submitButton);

      // .. add listeners etc

      // Add the wrappers to the view
      add(elementWrapper);
      add(buttonWrapper);

   }

}

This obviously works, however I believe that this is an ugly solution since now the view owns all the elements, instead of the wrappers – which I find more appropriate. More on, now all (i.e) GridBagConstraints has to be placed in the view.

One solution could of course be to place all the elements in the appropriate wrappers class, and then make it either public + final, or create a bazillion getters & setters.

However, I’m sure there is a more pretty OOP way to achieve this?

  • 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-17T07:06:13+00:00Added an answer on June 17, 2026 at 7:06 am

    Well, I don’t believe there is a silver bullet solution here.

    When I used to work with Swing I tried to keep in mind the following:
    – Use the encapsulation. Treat your Panel as a reusable component.
    – Decouple the data layer from the view as much as possible.

    When applied to your question this might mean the following:
    – The only thing that your AppUserView panel knows are 2 wrappers: elementWrapper and buttonWrapper. These are “black-boxes” – reusable components that can be used “as they are” by the AppUserView.
    Each “wrapper” has a reference on button/textfield/whatever it needs.
    The data model should be completely decoupled from these views, the only interaction should should be done is by events, when the model decides that it should fire some events, relevant views should be registered as listeners to such events and react respectively.

    Hope this helps

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

Sidebar

Related Questions

Using Java I have created RSA keypairs. Using Java I can use these keys
I am using Java version 1.6.0_21. These are the memory options I have set:
Using Java IO streams, it is quite often we use objects solely as constructors
I'm using java.awt.Robot for integration tests of my Swing application, but I'm having trouble
Using Java have the source code of a webpage stored in a string. I
Using Java 1.4.2 with unlimited jurisdiction policy files installed. I have a class that
Using Java, I have a class which retrieves a webpage as a byte array.
Using java for Android development. I have an array that stores image resources. The
Using Java,I have to fetch multiple sets of values from an XML file to
Using JAVA framework i want to achieve the following task. hot code(jar) deployment which

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.