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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T22:23:39+00:00 2026-05-20T22:23:39+00:00

As a contrast to this wiki , I am looking for the proper way

  • 0

As a contrast to this wiki, I am looking for the proper way to implement Swing GUI controls from a coding standpoint.

I have been on a quest to learn Java and its GUI tools but I find internet tutorial after internet tutorial that throws everything in main and I know this isn’t right.

I’ve also tried RAD systems like Netbeans and other “visual” editors but by the time I get to coding I’ve got a heap of code that I don’t know half of what it does, so I’m intent on learning to hand code swing, and I know the basic controls and layout, but want to do it the right way.

Is there a model or standard I’m missing?

example questions…

do I extend JFrame and create my own frame object? (I would assume yes)

do I encapsulate the main menu inside that frame object? or do I create its own? etc…

How to I separate “View” logic from “Application” logic?

Basically, I’m looking for what the industry standard is, on how to organize GUI code.

  • 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-20T22:23:40+00:00Added an answer on May 20, 2026 at 10:23 pm

    Since there seems to be some argument about what constitutes “best practices”, I’ll give you what I have found works best for me, and my reasoning:

    1.
    Each window should extend either JFrame or JDialog (depending on the type of window). This makes it easy to control the properties of the window without specifying a specific object every time. This is more of the general case, though, as I have been known to do it both ways.

    2.
    The main() method should be in a separate class. This increases the likelihood of being able to use your window classes elsewhere, as they are not tied to specific implementations. Technically it doesn’t make a difference, but application startup code just doesn’t belong in a window.

    3.
    Listeners should be in anonymous inner classes. Your top-level class should not implement any listeners. This prevents hacks like calling the listener methods from anywhere except the object to which they are attached.

    Here is a simple application with a single frame to demonstrate these practices:

    public class Main {
        public static void main(String[] args) {
            final String text = args[0];
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    final MyWindow wnd = new MyWindow(text);
                    wnd.setVisible(true);
                }
            });
        }
    }
    
    public class MyWindow extends JFrame {
        public MyWindow(String text) {
            super("My Window");
    
            setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
            addWindowListener(new WindowAdapter() {
                @Override
                public void windowClosing(WindowEvent e) {
                    MyWindow.this.setVisible(false);
                    MyWindow.this.dispose();
                }
            });
    
            final JButton btn = new JButton(text);
            btn.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    JOptionPane.showMessageDialog(MyWindow.this, "Button Pressed", "Hey", JOptionPane.INFORMATION_MESSAGE);
                }
            });
    
            setLayout(new FlowLayout());
            add(btn);
            pack();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a way to change the appearance of an icon (ie. contrast /
Please if you have used NHibernate and Entity Frameworks, please contrast your experiences.
I have a contract in the offering from a client to develop an intranet
I have installed a script on my website that allows for a low contrast
I am loading a series of controls in the Page_Load event. However, I have
What does this mean in contrast to unreachable code detected?
I have a program that will load an image from the hard disk. The
Running a ServiceHost with a single contract is working fine like this: servicehost =
In contrast to most software development organizations, our little research group within a university
Could someone compare and contrast on WCF Rest services vs. ADO.NET Data Services? What

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.