I created a UI with the UIBuilder.
Instead of always using the findXXXX ()
, I would love to do something like this in the Statemachine
:
public class Statemachine ... {
private Container c;
private Tabs t;
initVars () {
initAll();
}
....
private initAll () {
Container c = findContainer();
Tabs t = findTabs();
...
}
}
and use them as c.getYYY()
.
Is this possible? If yes, how?
Yes you can do that, but not advisable.
The danger of it is when you have 2 components with the same name on different forms, this may reference wrong component and may be hard to debug.
If you sure can deal with the above, you can declare and initiate the variables inside Statemachine class but outside of all methods as a private global variable.
public class Statemachine ... {
private Container c = findContainer();
private Tabs t = findTabs();
initVars () {
}
....
}
You can't do that in the current GUI builder since the current GUI builder creates forms dynamically and discards them during navigation.
The new GUI builder uses a completely different architecture and will work exactly like that: 1 class per one UI form. So you will have variables for every field.
You can see this in action here.
Note that at this time the GUI builder is still in pre-beta stage although we are heavily committed to it and its architecture.
来源:https://stackoverflow.com/questions/35057483/how-to-use-variables-to-refer-to-components-in-codename-one