• Post Reply Bookmark Topic Watch Topic
  • New Topic

Possible to dynamically declare and name variables?  RSS feed

 
Andreas Macke
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm building an app that allows users to set up business rules on a tier model. Each tier will allow selectio of a threshold value for an independent variable, and will allow selection of a corresponding value for a dependent variable (think of a case such as 80% attendance gets you 50% credit, 90 % attendance gets you 75% credit, etc.)
The actual tier structures will be stored as a TreeMap, with the dependent variable being the index and the independent variable the content (I can rely on the sorting that way during processing).Now I need to build a GUI for that, so I need discrete variables for the fields that will display these values (for unrelated reasons, I don't want to use a table), and I don't want to be restricted to having a fixed max number of tiers. Instead, I'd like to be able to loop through the iterator of the TreeMap's keySet() and then assign the variable names on the fly. Something like this:
Iterator it = myTreeMap.keySet().iterator();
int counter = 0;
while (it.hasNext()){
i++;
JTextField xField<i> = new JTextField;
xField<i>.setText(<based on something I retrieve from my TreeMap> ;
etc.
}
The crux here is the "xField<i>" part - I'd like to be able to generate variables on the fly and then do something with them. So if my TreeMap happens to have three members, I'd end up with variables xField1, xField2, xField3; whereas ten members would result in xField1, xField2, ... all the way through xField10.
Does Java have a way to generate individual variables this way?
TIA,
Andreas
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is no simple way to generate reference variable names for a class at runtime, but why bother. You are already close by using the TreeMap - why not a TreeMap containing JTextField references mapped by your naming scheme.
Since JTextField is serializable you can even write the whole collection to disk for storage between sessions.
Bill
 
Andreas Macke
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by William Brogden:
There is no simple way to generate reference variable names for a class at runtime, but why bother. You are already close by using the TreeMap - why not a TreeMap containing JTextField references mapped by your naming scheme.
Since JTextField is serializable you can even write the whole collection to disk for storage between sessions.
Bill

Hmmm... so I guess I could save all the redundant hardcoding of fieldnames in the actual application, but I'd still have to do it once to generate all the fields for a TreeMap, then serialize the TreeMap and pull it out whenever needed? I guess the only thing left is to make sure that the TreeMap has a high-enough number of tiers so I never run out. Not quite as a elegant as being able to just generate them as needed, but way better than what I had thought of (hardcoding...).
Am I getting what you're saying, or am I barking up the wrong tree here?
Thanks,
Andreas
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!