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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T10:48:22+00:00 2026-06-12T10:48:22+00:00

There are several examples on how to make a simple GUI. The question I

  • 0

There are several examples on how to make a simple GUI. The question I have is what is considered the proper way of doing this, and what is the rationale of doing it either way?

I have been using Window Builder Pro under Eclipse, and examining the code that is being generated. Apparently Window Builder prefers to declare components (buttons etc.) as local variables within the main method of the visual class. However there is a button in Window Builder that allows conversion to field. Most examples that I have come across seem to prefer field declaration.

Also many examples show that it is better to create a separate class that extends JFrame. Window Builder just declares JFrame as a field in the class.

Also should I have a single handler for many events or multiple handlers. Window Builder declares anonymous handlers for each event.

I just want to be clear on what the preferred approach is.

  • 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-12T10:48:24+00:00Added an answer on June 12, 2026 at 10:48 am

    Is there a specific reason to declare GUI components as fields?

    I’d say that you only need to do this if you are going to need to refer to that component somewhere in your program. For example this is usually not the case for most garden-variety JLabels, and so these are probably best declared and used at site of use. Many JButtons and JTextComponents however will need to be used elsewhere and these will likely be best declared as private class fields.

    Also should ActionListener be anonymous?

    Only if it makes sense to do so. To clarfiy, many/most quick one-off ActionListeners should be anonymous, but larger ones should be private inner classes, and complex larger ones should be separate stand-alone classes. I know of no simple test to distinguish between these other than feel and experience.

    I have been using Window Builder Pro under Eclipse, and examining the code that is being generated…

    If you’re an apprentice Swing coder, you’ll probably want to put this tool to the side a bit until you are very familiar with creating Swing GUI’s, as GUI builders can be tricky to use if you’re not quite familiar with the underlying library. Otherwise if you’re a journeyman and certainly if you’re an expert Swing programmer, than sure, go ahead and continue to use it.

    Also many examples show that it is better to create a separate class that extends JFrame. Window Builder just declares JFrame as a field in the class.

    Most experts in the Swing field that I know suggest that it’s usually best to not extend JFrame at all, but rather to create and use a JFrame object when needed. There are several reasons for this, but number one is to avoid unintended side effects such as overriding a critical method without realizing. This has happened to me when I’ve overridden a JComponent and gave it int getX() and int getY() methods. The component would never stay where it was supposed to be located!

    Also should I have a single handler for many events or multiple handlers. Window Builder declares anonymous handlers for each event.

    Most folks I know use a single handler to handle a single type of action. For instance, if you’re creating a calculator, it makes sense to create a single handler to be shared by all the number buttons, and perhaps several for the different types of operations: one handler for the basic math operations such as +, -, *, /, and one for the memory operations, and one each for more complex operations.

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

Sidebar

Related Questions

There are several other questions about this topic that I have gone through, but
OK, I am sure that there is a simple way to do this but
I am trying to understand how to use reference parameters. There are several examples
There have been several questions posted to SO about floating-point representation. For example, the
There are several threads on this here at SO but I didn't find one
There are several variant questions on this on SO, but I did not find
Have a simple console app where user is asked for several values to input.
I'm trying to make a simple three question quiz in php. After the user
I have spent the last several hours scouring the internet looking for examples and
I know there are several other posts on this topic but they still leave

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.