Glad you found the problem! Actually, what you are doing would actually
work, if you didn't put your panel into the MainFrame.bxml, but attached
it after reading it. But, if the two are inseparable, then it is
usually better to read everything in one shot.
True, the "getNamedComponent()" method only searches direct
descendants. If you know the hierarchy ahead of time, you can just do a
recursive search on the children of your frame, descending into further
children if the child is a "Container". Someone else a while back
suggested having a recursive version of "getNamedComponent", which has
its merits, also. But, I think the methods below are actually easier.
What we've done (mostly) in our application is use the @BXML notation,
in combination with "serializer.bind(...)". If you assign a
"bxml:id=..." in your *.bxml files, and then define a @BXML Component
.... in your Java file and call the "bind" method after doing the
"readObject", then the variables in your Java code will be assigned the
values of the identified components, and you can access them directly.
This tutorial explains this method pretty well:
http://pivot.apache.org/tutorials/stock-tracker.ui.html
There are other ways, as well, to get a referenced to a component loaded
via "bxml", such as getting the component map from the BXMLSerializer
via the "getNamespace()" method and then calling "get(componentName)" on
that map.
HTH,
~Roger
On 8/2/14 2:50 PM, ninjathehunter wrote:
Hi Roger
Many thanks for your reply.
I have just found the cause of this. Basically, I called the readObject
twice, one on the frame that contains MyPanel, and one on the MyPanel
itself. This created two instances of MyPanel unfortunately. The listener I
added is added to the second instance, which is a standalone instance, not
attached to the main frame.
(MainFrame) bxmlSerializer.readObject(MainFrame.class,
"/gui/MainFrame.bxml"));
(MyPanel) bxmlSerializer.readObject(MyPanel.class, "/gui/MyPanel.bxml"));
I would also like to ask it is possible to get grand-child component of a
component (a component that is not a direct child of a component).
For example, in the above example: MainFrame contains MyPanel, MyPanel
contains MyButton.
Is it possible to get MyButton directly from the instance of MainFrame?
I tried myFrame.getNamedComponent() method. But it looks like it can only be
used to get direct child of a component.
Thank you very much.
--
View this message in context:
http://apache-pivot-users.399431.n3.nabble.com/Cannot-config-a-button-press-listener-outside-the-initialize-method-of-Bindable-tp4022979p4022981.html
Sent from the Apache Pivot - Users mailing list archive at Nabble.com.