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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T12:48:56+00:00 2026-06-02T12:48:56+00:00

I am building a graphical user interface in Java and I was wondering if

  • 0

I am building a graphical user interface in Java and I was wondering if anyone can give me some directions.
Specifically, When clicking on a button in this GUI a big JDialog (it’s a class that extends a JDialog) opens up for the user to enter some information, see some information, click on some things etc.

This class has multiple JLabels and other components in it. My question is how would one create such an instance? Right now I have everything in the constructor, making it a 500(!) lines constructor. There has to be an alternative to that!
The constructor is about 300 lines of code of components placement and settings for them and another 200 lines for the listeners give or take.

Also another question that comes in mind is that right now I open this window from another class by calling

MyClassExtendsJDIalog temp = new MyClassExtendsJDIalog();

but I don’t use this “temp” variable at all in the class that I create it, as the constructor of “temp” does EVERYTHING. It seems like I am, again, doing something the wrong way.

Thanks for reading 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-02T12:49:04+00:00Added an answer on June 2, 2026 at 12:49 pm

    Yes you are doing something wrong, but you are in good (or bad) company.

    You should apply the rules of good OO design and clean code to your swing classes as to anything else.

    What exactly you can do is hard to tell without looking at the 300+ lines of code (and I really don’t want to see the 😉 but here are somethings that are probably applicable.

    My main design rule is the Single Responsibilite Principle. According to your description (and my guesswork), you are doing the following in your constructor:

    * creating (including configuring) components
    
    * placing them in some kind of layout
    
    * registering Listeners
    
    * implementing Listners
    

    These are 4 completely different concerns.

    After some heavy refactoring you might end up with something like this:

    You might want to have a factory that creates your components.

    You might have a Layouter class that takes a component or a group of components (like label plus matching textbox) and places them on a panel.

    You might have a class takes components (and maybe models or whatever) and wires them together using Listeners.

    And a class that gets passed all this uses it in a suitable way and spits out a JDialog with the panel with all your components on.

    Note: Idealy nothing extends JDialog anymore. You should only extend Janything if you intend to build a new Swing component.

    This example is using Scala but it still should offer some inspiration: http://blog.schauderhaft.de/2011/06/26/clean-code-with-swing-and-scala/

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

Sidebar

Related Questions

There are a lot of flexible, complete, cross-platform, et cetera , graphical user interface
I'm building an application that allows the user to create a Human model (graphical
I'm building a simple wallpaper app where the user can browse through a selection
Building a lottery scratch-card app in Java. Have allot of it done, just need
Building the same project (without any changes) produces binary different exe-files: some small regions
Building on what has been written in SO question Best Singleton Implementation In Java
We're building an application for OS X, and we can't rely on our users
I was having some other problems with some ports building for the wrong CPU
I need to be able to locate the user in a building, show the
We are building a graphical application, we need to draw dots on the canvas

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.