Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Controlling where references to GUI objects created by Eclipse Window Builder are placed

Abstract: I'm looking for a way to configure Eclipse Window Builder's code generator to place its object references in the class body instead of locally in the initalize() method.

Detailed:

Window builder has a built in code generator/code analyzer which can generate GUI code from a 'graphical form designer' and vice versa. It is of great use to design GUIs quickly and learn how the various frameworks work in java (especially for java beginners). However, i am having trouble using the generated code in my design when moving towards more sophisticated GUI setups. The problem is that most of the generated code is being placed inside the initialize() method which makes the objects resident in the local scope and hard to get references to from the 'outside'.

So far i've been copy/pasting the type declarations from the initialize() method into the class body manually as public to have access to them, but this seems somehow the wrong approach. It works fine as long as i don't reorganize the GUI from within the Graphical Designer. In that case the code generator places it's type declarations again in the initialize() method.

Automatically generated code looks like this:

   // AUTO-GENERATED CODE
   public class MyGUI {
      private void initialize() {
          // gui objects declared AND created in this scope by Window Builder
          JFrame frame = new JFrame();
          JPanel panel_1 = new JPanel();
          frame.getContentPane().add(panel_1);
    }
  }

What i would like is:

   public class MyGUI {
          // accessible from outside
          public Frame frame = null;
          public JPanel panel_1 = null;

       private void initialize() {
          // gui objects only created in this scope
          frame = new JFrame();
          panel_1 = new JPanel();
          frame.getContentPane().add(panel_1);
    }
  }

I've been looking in the documentation for a solution (scarce documentation on that), and also studied the various Window Builder preferences but no luck so far. There is the 'data binding' but it seems to implement MVC (bind the gui elements to a model) and quite a overkill.

Is there a design pattern i'm missing, or am i supposed to 'fish' the references out of the local scope of initialize() somehow ?

like image 508
count0 Avatar asked Nov 03 '22 16:11

count0


1 Answers

In the Preferences window, expand WindowBuilder and Swing (or SWT) and then expand Code Generation. There are options to declare variables locally, as class-level fields, and lazily initialized. You can also choose flat or block statements. There are some examples in the documentation under WindowBuilder Pro User Guide > Preferences > Swing of what to look for.

like image 155
Thomas Owens Avatar answered Nov 09 '22 06:11

Thomas Owens