If you have a Java variable named xyz. Then later on I define a string which has the value of the named variable I want to play with.
String x="xyz";
How do I get Java to recognise that String x as a pointer to variable xyz?
An arbitrary example:
JButton a= new JButton();
Later on ...
String x="a";
I want to now say something like
JButton called string x.setPreferredSize(new Dimension(40,30));
We can assign character string to variable name by using assign() function. We simply have to pass the name of the variable and the value to the function. Parameter: variable_name is the name of the value.
String Variables. EES supports numerical and string variables. A string variable is identified with a variable name that ends with the $ character. A string array variable has the $ character just before the left bracket that holds the array index.
Variables are containers for storing data values. In Java, there are different types of variables, for example: String - stores text, such as "Hello". String values are surrounded by double quotes.
To print a variable's name:Use a formatted string literal to get the variable's name and value. Split the string on the equal sign and get the variable's name. Use the print() function to print the variable's name.
In general, if you want to access a variable in this way, you have to use Reflection, which is slower and potentially dangerous.
However, since you have a very specific scenario in mind, I'd take a different approach. Why not put your buttons or other elements into a map, with keys that are strings:
Map<String, JComponent> currentComponents = new HashMap<String, JComponent>();
currentComponents.put("a", new JButton());
String x = "a";
currentComponents.get(x).setPreferredSize(new Dimension(40,30));
The short answer is that you can't do something like this. That just isn't the way the Java language works. The long answer is that you might be able to simulate something using the Reflection API.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With