Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Each Component can have a name, accessed via getName() and setName(), but you'll have to write your own lookup function.</p> <p>You can also try with HashMap. I find an example with that.</p> <pre><code> 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&lt;String,Component&gt;(); Component[] components = yourForm.getContentPane().getComponents(); for (int i=0; i &lt; 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. </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload