• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Dynamic component processing based on a value

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, everybody!

Pls. help me with this scenario.

I want to be able to set a property of any component of a window (which contains several components) from the value of a variable which was populated earlier but this "setting" process will be applied to "any" of the component in the window based on the value of the variable. If value of variable is "a", I'll set component a, if "b" I'll set component b, and so on... Programmatically, I need to have series of IFs then set accordingly. What I exactly want to do is to populate the value of the variable with the "component name" so that I can just have a line of code something like this "variable value".setProperty(...)

Thanks in advance!
 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hmmm. Off the cuff, I'd say the easiest way to do this would be to create a HashMap, placing the components as values, with their name as the key. It would then be trivial to create a method that returns the component given the component name.

Haven't really thought this out - again, just sharing the first thing that comes to mind. It might help if you could post a bit of code; not only will it help others understand your question better, but the answers may come to you as you prepare the sample.

Good Luck
 
J Roxas
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Todd,

Here's a simple example of what I'm asking.

Assuming I have 3 jtextfields named tf1, tf2 and tf3 and a jlist named jlstChoice which was constructed with the values of "1","2","3". When the user clicks on "1" from the jlstChoice, the text of tf1 will be set to the string "Chosen Field", if the user clicks on "2", the text of tf2 will be set to the same string and with "3", tf3 will be set to the same string.
Here's the code for the mouseClicked event of jlstChoice.

{
String txtToPrint = new String("Chosen Field");
String chosenValue =
new String(jlstChoice.getSelectedValue().toString());

if (chosenValue.equals("1"))
{tf1.setText(txtToPrint);}
else if (chosenValue.equals("2"))
{tf2.setText(txtToPrint);}
else {tf3.setText(txtToPrint);}
}


What I wanted is just initialize jlstChoice with the values
"tf1", "tf2", "tf3" and whenever the user clicks on any of the choices I am only going to code like this:

{
String txtToPrint = new String("Chosen Field");
String chosenValue =
new String(jlstChoice.getSelectedValue().toString());

//chosenValue now has either "tf1","tf2","tf3"

CHOSENVALUE.setText(txtToPrint); //here's the code I'm asking if possible
//it is something like evaluating its
//value and using it as the component
//name
}
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My solution of choice would be to put *the textfields themselves* into the JList, and register a renderer with it that renders the textfields names (set via setName on the textfields).

If you don't want to do that, a HashMap sounds like the second best solution...
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
set your textfields up as an array tf[0],tf[1],tf[2]
your JList has values 1,2,3 (the indexes being 0,1,2)

then it's just

tf[jlstChoice.getSelectedIndex()].setText(txtToPrint);

but you should wrap the above in an if statement, to ensure the
jlstChoice selectedIndex is > -1 (ie no selection)
 
J Roxas
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks guys for your prompt replies! I'll work on with your suggested solutions.
 
Ilja Preuss
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Michael Dunn:
set your textfields up as an array tf[0],tf[1],tf[2]
your JList has values 1,2,3 (the indexes being 0,1,2)

then it's just

tf[jlstChoice.getSelectedIndex()].setText(txtToPrint);

but you should wrap the above in an if statement, to ensure the
jlstChoice selectedIndex is > -1 (ie no selection)



At first sight this sounds like the simplest solution of the ones presented, but in my view it's also the most error prone and least maintainable one, because the mapping between the list entries and the textfields is the most indirect one. Just imagine that you want to introduce a "1.a" entry or something...
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic