Reputation: 2100
I need to be able to have a list of components in a class and their respective variable name. I did manage to get the list of all the components in the GUI. However when I call the getName method for the component, a null is being returned. Does anyone know how to be able to get such a name for a component please?
This is the code so far:
public static void main(String[] args){
Calculator c = new Calculator();
List<Component> containers = getAllComponents(c.getContentPane());
for (int i = 0; i < containers.size(); i++) {
System.out.println(containers.get(i).getClass().getName());
System.out.println(containers.get(i).getName());
}
System.exit(0);
}
public static List<Component> getAllComponents(final Container c) {
Component[] comps = c.getComponents();
List<Component> compList = new ArrayList<Component>();
for (Component comp : comps) {
compList.add(comp);
if (comp instanceof Container) {
compList.addAll(getAllComponents((Container) comp));
}
}
return compList;
}
Upvotes: 1
Views: 4588
Reputation: 137
Each Component can have a name, accessed via getName() and setName(), but you'll have to write your own lookup function.
You can also try with HashMap. I find an example with that.
Create a Map class variable. You'll need to import HashMap at the very least. I named mine componentMap for simplicity.
private HashMap componentMap;
Add all of your components to the frame as normal.
initialize() {
//add your components and be sure
//to name them.
...
//after adding all the components,
//call this method we're about to create.
createComponentMap();
}
Define the following two methods in your class. You'll need to import Component if you haven't already:
private void createComponentMap() {
componentMap = new HashMap<String,Component>();
Component[] components = yourForm.getContentPane().getComponents();
for (int i=0; i < components.length; i++) {
componentMap.put(components[i].getName(), components[i]);
}
}
public Component getComponentByName(String name) {
if (componentMap.containsKey(name)) {
return (Component) componentMap.get(name);
}
else return null;
}
Now you've got a HashMap that maps all the currently existing components in your frame/content pane/panel/etc to their respective names.
To now access these components, it is as simple as a call to getComponentByName(String name). If a component with that name exists, it will return that component. If not, it returns null. It is your responsibility to cast the component to the proper type. I suggest using instanceof to be sure.
Upvotes: 0
Reputation: 328556
getName()
will only return a value if you called setName()
before. But that name is arbitrary. It's a debugging aid for UI developers.
My guess is that you want to know which component is in which field of the Calculator
class. For this, you need to use the Reflection API.
Try Calculator.class.getDeclaredFields()
. To read the value of a private field, see this example code.
Upvotes: 1
Reputation: 80603
The getName()
method for components is not guaranteed to return a value, and certainly does not return the variable name the component was assigned too. Also note that it's possible for several variables to point to the same object instance, so that information would be less than useful even if it was available.
Upvotes: 1