If I know what the name of the component is which was set by component.setName(name) how can I use the name to get the component object.
if you have added that component via designer then you can directly access by
findWhatEverComponentName()
(with below TextField name you can access by findMyTf())
but if you have added component by code then one of the method to access the component is by its parent container as (for example its TextField then)
TextField myTf = (TextField) findParentContainer().getComponentAt(0)
here instead of 0 you have to maintain correct component index from that ParentContainer.
and to compare its name you can use findParentContainer().getComponentAt(0).getName().equalsIgnoreCase(name)
E.g.:
private static Component findByName(Container root, String componentName) {
int count = root.getComponentCount();
for(int iter = 0 ; iter < count ; iter++) {
Component c = root.getComponentAt(iter);
String n = c.getName();
if(n != null && n.equals(componentName)) {
return c;
}
if(c instanceof Container) {
c = findByName((Container)c, componentName);
if(c != null) {
return c;
}
}
}
return null;
}
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments