0

I need to execute this instruction:

jButton1.setBackground(Color.orange);

but , jButton1 comes by a variable that i recover from a Data Base.

String b="jButton1";

and now I have to execute :

  ***b + ".setBackground(Color.orange)" ***

But Eval function is not aplicable. How can I execute this String *** boton + ".setBackground(Color.orange)"*** as an instruction?

5
  • 1
    Does this answer your question? Is there an eval() function in Java? Commented Feb 3, 2021 at 11:05
  • what is exactly "to execute" ? And what is the "Eval" function you mentioned here? Commented Feb 3, 2021 at 11:08
  • 1
    Short answer: you can't. Also your design looks like a big code smell. Storing variable names in a database? Awful idea. Commented Feb 3, 2021 at 11:10
  • 2
    You are probably looking for what is called "Reflection" in java. (See similar thread here) With this, you can get the field via the variable name on runtime. Commented Feb 3, 2021 at 11:10
  • Maybe GroovyShell will help? Commented Aug 19, 2021 at 10:37

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.