Get list of components

How can I get a list of all the component names in a Screen using java such as name of the labels, text boxes etc. Please help as soon as possible.

Can you provide an example

This is the code I've used in manyof my extensions.
Do not call it directly from your extension Constructor, it may fail to map all the components.

components is a mapping of Component Name to Component Instance.
You may modify the code to only have the component names list.

Map<String, Component> components;
if (form instanceof ReplForm) {
  components = mapComponentsRepl();
} else {
  components = mapComponents();
}

...

private Map<String, Component> mapComponents() throws NoSuchFieldException, IllegalAccessException {
    Map<String, Component> components = new HashMap<>();
    Field componentsField = form.getClass().getField("components$Mnto$Mncreate");
    LList listComponents = (LList) componentsField.get(form);
    for (Object component : listComponents) {
      LList lList = (LList) component;
      SimpleSymbol symbol = (SimpleSymbol) lList.get(2);
      String componentName = symbol.getName();
      Object value = form.getClass().getField(componentName).get(form);
      if (value instanceof Component) {
        components.put(componentName, (Component) value);
      }
    }
    return components;
  }

...

private Map<String, Component> mapComponentsRepl() throws NoSuchFieldException, IllegalAccessException {
    Map<String, Component> components = new HashMap<>();
    Field field = form.getClass().getField("form$Mnenvironment");
    Environment environment = (Environment) field.get(form);
    LocationEnumeration locationEnumeration = environment.enumerateAllLocations();
    while (locationEnumeration.hasMoreElements()) {
      Location location = locationEnumeration.next();
      String componentName = location.getKeySymbol().getName();
      Object value = location.getValue();
      if (value instanceof Component) {
        components.put(componentName, (Component) value);
      }
    }
    return components;
  }
5 Likes

Thanks

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.