Control Arrays?

SWT Designer allows you to create the views, editors, perspectives, pref pages, composites, etc. that comprise Eclipse SWT & RCP applications and plug-ins.

Moderators: Konstantin.Scheglov, gnebling, Alexander.Mitin, jwren, Eric Clayberg

Control Arrays?

Postby kgregg » Thu Aug 19, 2010 9:30 am

WB Design pane doesn't show control arrays. For example, source code:

Label[] labels = new Label[6];
for(int i = 0; i < labels.length; i++) {
labels[i] = new Label(composite, SWT.NONE);
labels[i].setText("Label" + i);
labels[i].setBounds(10, 10 + i*26, 251, 18);
}

Shows nothing on the Design pane, but works fine upon execution.

Am I missing something?

Thanks
kgregg
 
Posts: 14
Joined: Thu Aug 19, 2010 9:22 am

Re: Control Arrays?

Postby Eric Clayberg » Thu Aug 19, 2010 9:36 am

This is quite intentional, and the product is working as intended.

The parser intentionally ignores any code within conditionals, loops or other non-static / dynamic constructs. See the FAQ...

http://download.instantiations.com/D2WB ... atedByHand
Eric Clayberg
Software Engineering Manager
Google
http://code.google.com/webtoolkit/download.html

Author: "Eclipse Plug-ins"
http://www.qualityeclipse.com
Eric Clayberg
Moderator
 
Posts: 4503
Joined: Tue Sep 30, 2003 6:39 am
Location: Boston, MA USA

Re: Control Arrays?

Postby kgregg » Thu Aug 19, 2010 9:58 am

Eric, thanks for the quick reply.

While, technically, the code example I posted before was not what I call dynamic, I modified it to hard-code the labels as such:

Label[] labels = new Label[6];
labels[0] = new Label(composite, SWT.NONE);
labels[0].setText("Label" + "0");
labels[0].setBounds(10, 10, 251, 18);
labels[1] = new Label(composite, SWT.NONE);
labels[1].setText("Label" + "1");
labels[1].setBounds(10, 10, 251, 18);
...

However, Designer still won't display the labels. Is the fact that they are defined as an array causing the problem?
kgregg
 
Posts: 14
Joined: Thu Aug 19, 2010 9:22 am

Re: Control Arrays?

Postby Eric Clayberg » Thu Aug 19, 2010 10:07 am

Arrays are typically considered to be variable, dynamic constructs and are intentionally ignored by the parser. Each widget should be assigned to its own variable, if you need to access it later.

If you need to process the fields in an array for some reason, assign them each to an array element after they have been created and assigned to their own unique vairables.
Eric Clayberg
Software Engineering Manager
Google
http://code.google.com/webtoolkit/download.html

Author: "Eclipse Plug-ins"
http://www.qualityeclipse.com
Eric Clayberg
Moderator
 
Posts: 4503
Joined: Tue Sep 30, 2003 6:39 am
Location: Boston, MA USA

Re: Control Arrays?

Postby Eric Clayberg » Thu Aug 19, 2010 10:07 am

Arrays are typically considered to be variable, dynamic constructs and are intentionally ignored by the parser. Each widget should be assigned to its own variable, if you need to access it later.

If you need to process the fields in an array for some reason, assign them each to an array element after they have been created and assigned to their own unique vairables.
Eric Clayberg
Software Engineering Manager
Google
http://code.google.com/webtoolkit/download.html

Author: "Eclipse Plug-ins"
http://www.qualityeclipse.com
Eric Clayberg
Moderator
 
Posts: 4503
Joined: Tue Sep 30, 2003 6:39 am
Location: Boston, MA USA

Re: Control Arrays?

Postby kgregg » Thu Aug 19, 2010 11:03 am

My app needs to dynamically build screen content from database tables, so using control arrays is highly desirable. I think what you are saying is that for WB to show controls at design time, they cannot be a control array. If I use un-arrayed controls just to get the design time display, I've then got to hard-code the process to load each individually with the database table values instead of using an index value, which is not desirable. For example, label0.setText = dbvalue[0], label1.setText = dbvalue[1], etc. instead of a for loop with: label[i].setText = dbvalue[i].

So I guess if I want a flexible screen load process using control arrays, I can't have the design time display and if I want the design time display, I can't have the flexible screen load process, right?
kgregg
 
Posts: 14
Joined: Thu Aug 19, 2010 9:22 am

Re: Control Arrays?

Postby Eric Clayberg » Thu Aug 19, 2010 12:41 pm

Right.

The design approach of using an array of DB values and accessing them via index might save you a couple of lines of code, but it is very fragile and difficult to maintain.

If the actual GUI is dynamic such that the widgets themselves change based on ten DB, then there isn't much point to doing a static layout using a GUI tool. If the widgets are fixed, your code will be much easier to read and maintain, if each widget has its own identity. If you want to access and load the widgets via an array, build your control array after you have created the individual widgets.
Eric Clayberg
Software Engineering Manager
Google
http://code.google.com/webtoolkit/download.html

Author: "Eclipse Plug-ins"
http://www.qualityeclipse.com
Eric Clayberg
Moderator
 
Posts: 4503
Joined: Tue Sep 30, 2003 6:39 am
Location: Boston, MA USA


Return to SWT Designer

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 1 guest

cron