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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T21:18:28+00:00 2026-05-25T21:18:28+00:00

WindowBuilder for Swing creates checkboxes as local variables and textboxes as member data. This

  • 0

WindowBuilder for Swing creates checkboxes as local variables and textboxes as member data. This inconsistency bothered me. Since it all gets linked up into the toplevel JFrame anyway, these widgets are sure to live as long as the JFrame has references down to them so there doesn’t seem to be a need for the textboxes to be member data. It seems to me that the textboxes should be locals just like the checkboxes. Locals are better encapsulation. The local references can die at the end of the GUI object (a class that extends JFrame) constructor generated by WindowBuilder and the JFrame will still have references to all the widgets.

Making them local and putting “final” in front of these widget declarations so that they can be used inside the event handler’s anonymous inner classes was what it took to make them work. I also had to rearrange the order a bit since the order of instantiation of textboxes doesn’t matter if they are all declared as members. Order does matter for locals so I had to move the uses of the “new” operator (instantiation) “up” a bit towards the top of the local scope. They just had to be north of the event handlers that use them.

So far I’ve found no bugs as a consequence so I am asking why WindowBuilder did not do it this way to begin with. I am new to Swing and WindowBuilder so there is a high probability that there are excellent reasons for WindowBuilder to not do this even though it seems to be the right approach for my case.

The following is the WindowBuilder output after some trivial naming modifications but before the modifications described above. This is the output as it is with 2 textboxes, 2 checkboxes, 2 buttons in the north and 1 label in the center. This is being pasted here in case somebody can see something here that may explain the choice behind WindowBuilder’s use of member data.

public class TestWB extends JFrame
{
    private static final long serialVersionUID = 1L;
    private JPanel contentPane;
    private JTextField textBox1;
    private JTextField textBox2;

    public TestWB() // the constructor
    { 
       ... // see the constructor below
    }
}

The constructor for the above class:

public TestWB()
{
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 646, 451);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    contentPane.setLayout(new BorderLayout(0, 0));
    setContentPane(contentPane);

    JPanel northPanel = new JPanel();
    contentPane.add(northPanel, BorderLayout.NORTH);

    JButton button1 = new JButton("button1");
    button1.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
        }
    });
    northPanel.add(button1);

    JButton button2 = new JButton("button2");
    button2.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
        }
    });
    northPanel.add(button2);

    final JCheckBox checkBox1 = new JCheckBox("cb1");
    checkBox1.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            CbProcessor cbp = new CbProcessor();
            cbp.dealWithCb(checkBox1.isSelected(), textBox1);
        }
    });
    northPanel.add(checkBox1);

    final JCheckBox checkBox2 = new JCheckBox("cb2");
    checkBox2.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            CbProcessor cbp = new CbProcessor();
            cbp.dealWithCb(checkBox2.isSelected(), textBox2);
        }
    });
    northPanel.add(checkBox2);

    textBox1 = new JTextField();
    textBox1.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
        }
    });
    textBox1.setText("tb1");
    northPanel.add(textBox1);
    textBox1.setColumns(5);

    textBox2 = new JTextField();
    textBox2.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
        }
    });
    textBox2.setText("tb2");
    northPanel.add(textBox2);
    textBox2.setColumns(5);

    JPanel centerPanel = new JPanel();
    contentPane.add(centerPanel, BorderLayout.CENTER);

    JLabel label1 = new JLabel("label1");
    centerPanel.add(label1);
}
  • 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-25T21:18:28+00:00Added an answer on May 25, 2026 at 9:18 pm

    This is a case where reading the WindowBuilder docs would have easily answered your question(s). WindowBuilder will generate code any way that you like. Widgets can all be local variables, all be fields, or any combination in between. You can control the scope of different widgets individually or on a type-by-type basis (by setting defaults). In fact, WindowBuilder has an incredibly rich set of code generation preferences and can replicate just about any code generation style you could desire. It will also happily reverse engineer just about any code you throw at it, so you can make just about any changes you want to the generated code (by hand or via the tool) and it will remain perfectly happy.

    Swing Code Generation Preferences

    Swing Variable Preferences

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

Sidebar

Related Questions

I have a college project based on java swing. I wanted to use windowbuilder
I'm in a project with WindowBuilder Pro, and while trying to get a good
Having recently heard about Windowbuilder and its excellent graphical tools, I am looking atways
Can I use WindowBuilder (a Java visual editor Eclipse plugin) without Google Web Toolkit
I am programming the admin part of a Swing application using WindowBuider, it would
I'm developing an application in Eclipse and WindowBuilder. I tried to add a look
This question has received a total of several paragraphs of answer. Here is the
i'm using Windowbuilder in Eclipse indigo. I have a main JPanel, and I want
I am using WindowBuilder Pro for eclipse, and I would like to have two
I've been messing with windowbuilder pro lately. I've lately run into the problem of

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.