Search code examples
javaswingnetbeans

Using getComponent() in swing to call a variable


Alright this may seem like a weird question but is there a way that after calling something like

jPanel3.getComponent(0).getName();

That I can use that value to make a call on a variable. Basically if it returns say jLabel1. That I can use that to call something on that label such as .setText("Hi"); Instead of having to type jLabel1.setText("hi"). Meaning can I use the returned value to directly call a function on it.


Solution

  • If I understood the question correctly, you want something like this:

    Component c=jPanel3.getComponent(0);
    if (c instanceof JLabel)
        ((JLabel)c).setText("hi");