Finding programmatically created Components by assigned name in a Container hierarchy

63 views
Skip to first unread message

timo.v...@gmail.com

unread,
Dec 13, 2013, 5:04:15 AM12/13/13
to codenameone...@googlegroups.com
I am programmatically creating a part of a Form, and want to retrieve entered values after the form is closed.
For this purpose I need to find a few specific TextField Components from the layout hierarchy.

Is it possible to find these Components by their assigned name, avoiding the introduction of lookup tables and without using fragile getComponentAt() calls to parse the hierarchy?

(The findByName() function of UIBuilder apparently only finds named Components that were added in the Designer.)

Shai Almog

unread,
Dec 13, 2013, 2:21:22 PM12/13/13
to codenameone...@googlegroups.com, timo.v...@gmail.com
Are you looking for Component.putClientProperty/getClientProperty?
That's what findByName uses internally.

http://www.codenameone.com/3/post/2013/10/property-of-the-client.html

timo.v...@gmail.com

unread,
Dec 15, 2013, 6:03:19 AM12/15/13
to codenameone...@googlegroups.com, timo.v...@gmail.com
A quick glance at the source code showed me all kinds of mystic clientproperties being set, including the "%"+name+"%" name references to components. To me this looked like UIBuilder magic I do not want to mess with.

Why is there no findByName() method in Container that uses the (already existing) name attribute of components?

Shai Almog

unread,
Dec 15, 2013, 2:22:23 PM12/15/13
to codenameone...@googlegroups.com, timo.v...@gmail.com
That's a special case for the UI builder so handcoded components will not have it. We have no way of knowing which component you add to which hierarchy and how you will mutate said hierarchy.

We use client properties for our needs, that doesn't mean you shouldn't use it too.

timo.v...@gmail.com

unread,
Dec 15, 2013, 2:47:23 PM12/15/13
to codenameone...@googlegroups.com, timo.v...@gmail.com
But why is there no findByName() method in Container that uses the (already existing) name attribute of components?

Shai Almog

unread,
Dec 16, 2013, 1:17:15 AM12/16/13
to codenameone...@googlegroups.com, timo.v...@gmail.com
Because people would use it and its not a good idea for most use cases.

Implementing it in the Container class will give no benefit since this method can easily be implemented externally, it would just clutter the API.

For GUI builder apps you have the more efficient findByName that works correctly. For the handcoded apps you can easily handle the component pointer which is more efficient.
For the edge cases of dynamically created UI you can handle a strategy of put/getClientProperty (what we do in the UI builder) which is superior.
Reply all
Reply to author
Forward
0 new messages