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?

İsmail Y.
  • 3,579
  • 5
  • 21
  • 29
  • 1
    Does this answer your question? [Is there an eval() function in Java?](https://stackoverflow.com/questions/2605032/is-there-an-eval-function-in-java) – user1717259 Feb 03 '21 at 11:05
  • what is exactly "to execute" ? And what is the "Eval" function you mentioned here? –  Feb 03 '21 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. – m0skit0 Feb 03 '21 at 11:10
  • 2
    You are probably looking for what is called "Reflection" in java. (See similar thread [here](https://stackoverflow.com/a/38539512/11441011)) With this, you can get the field via the variable name on runtime. – maloomeister Feb 03 '21 at 11:10
  • Maybe GroovyShell will help? – Grzegorz Aug 19 '21 at 10:37

0 Answers0