Maybe you should use an interface that defines a getButton() method. That would 
be more type-safe.

On Feb 2, 2011, at 8:24 AM, Aanjaneya Shukla wrote:

> Okay, but if I have multiple Window object and I randomly load an object into 
> my frame. In this case I have access to only the Container object and if I 
> have to suppose disable a button in my window then I need the Button object. 
> And in this case I don't have location of bxml file, hence I don't think 
> BXMLSerializer can be used.
> 
> Thanks
> 
> ________________________________________
> From: Greg Brown [[email protected]]
> Sent: Wednesday, February 02, 2011 20:53
> To: [email protected]
> Subject: Re: getting object fron its container
> 
> Actually, because the BXML ID value is mapped to Component#setName() via the 
> IDProperty annotation, you could also get the button in the example below via 
> getNamedComponent("Button1").
> 
> However, as Chris noted, getNamedComponent() is not recursive. If you want 
> access to all values declared with IDs in your BXML file, you should use 
> BXMLSerializer.getNamespace().get() or implement Bindable in your root 
> element.
> 
> G
> 
> On Feb 2, 2011, at 5:06 AM, Chris Bartlett wrote:
> 
> The Container#getNamedComponent(String) method is defined here
> http://svn.apache.org/repos/asf/pivot/trunk/wtk/src/org/apache/pivot/wtk/Container.java
> 
> You can see that it only iterates over the direct child Components in the 
> Container and then checks for a matching name property.
> It does *not* check to see if those Components are actually instances of 
> Container, and then recursively iterate over them.
> 
> It appears that none of the standard Pivot Containers override this method at 
> look into child Containers, so you would need to roll your own method if you 
> need to do that.
> 
> 
> Bear in mind that theContainer#getNamedComponent(String) method attempts to 
> match against Component#getName().
> BXMLSerializer sets the Component's name property to the same value as the 
> bxml:id as a convenience, but these are 2 separate values.
> 
> The Component's name can be set explicity in BXML as follows.
> <PushButton bxml:id="Button1" name="Foo" buttonData="%Button1”/>
> or in java (including at runtime) with
> myComponent.setName("Foo");
> 
> If you wanted to find this PushButton by name you would have to supply a 
> value of "Foo", and not "Button1"
> 
> 
> It will often be more convenient to use the @BXML annotation to pick up 
> objects in your code.
> The section titled 'The Bindable Interface' in the following tutorial 
> contains more info.
> http://pivot.apache.org/tutorials/stock-tracker.ui.html
> 
> Chris
> 
> On 2 February 2011 17:43, Aanjaneya Shukla 
> <[email protected]<mailto:[email protected]>> wrote:
> Hi,
> I am having some issues using ‘getNamedComponent’ method.
> 
> BXML file:
> <PushButton bxml:id="Button1" buttonData="%Button1”/>
> 
> Java file:
> PushButton Button1 = (PushButton)window.getNamedComponent("Button1");
> 
> I want to get Button object but I have getting null value returned.
> 
> 

Reply via email to