Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / December 2003

Tip: Looking for answers? Try searching our database.

Access an object by its name as string

Thread view: 
Wiz - 22 Dec 2003 11:23 GMT
Hi there,
I have some JTextField objects, called jtf0, jtf1, jtf2, and so on.
How I can access their properties by their names known as string? For
instance:

for(int i=0; i<10; i++) {
 function_to_get_a_reference("jtf" + i).setText("hello " + i);
}

Does a function like this exist? With other languages I'd use the eval()
function, but I couldn't fint it in Java.
Thanks in advance to everyone can help me.
Bye

Marco

ps: sorry for my bad english..
Christophe Vanfleteren - 22 Dec 2003 12:02 GMT
> Hi there,
> I have some JTextField objects, called jtf0, jtf1, jtf2, and so on.
[quoted text clipped - 13 lines]
>
> ps: sorry for my bad english..

No, you can't do that (not without using something like reflection anyway,
which would be a bad solution for things like this).

Just put all your textfields in an array.

something like:

JTextField[] txtfields = new JTextField[3];
//arrays must be explicitly initialized for objects:
for (int i = 0;i<txtfields.length;i++) {
       txtfields[i] = new JTextField();
}

Later on you loop over that same array and set the text:

for (int i = 0;i<txtfields.length;i++) {
       txtfields[i].setText("Hello " + (i+1));
}

Signature

Kind regards,
Christophe Vanfleteren

Antti S. Brax - 22 Dec 2003 12:09 GMT
wiz@near.it wrote in comp.lang.java.gui:
> I have some JTextField objects, called jtf0, jtf1, jtf2, and so on.
> How I can access their properties by their names known as string? For
> instance:
<snip>

    Let me get this straight: You have a class which has attributes
    named "jtf0", "jtf1" and so on. These attributes are of type
    JTextField and contain references to text fields displayed in
    your GUI. Right?

    The wrong way to do it is to declare the attributed public and
    use reflection. Class.getField(String) returns a Field object
    which has a get(Object o) method. This method returns the value
    of the specified field in object o.

    The right way is to it is to redesign the GUI a bit and put the
    JTextField objects into a Map with appropriate keys. Or if all
    the fields perform a similar function you could put them into
    an array.

    You have an idea about how you want to do something. Please tell
    us what you want to do because the "how" you have figured out is
    propably not a very good idea...

Signature

Antti S. Brax - asb(at)iki.fi   Rullalautailu pitää lapset poissa ladulta
  http://www.iki.fi/asb/          http://www.cs.helsinki.fi/u/abrax/hlb/

Vincent Vollers - 22 Dec 2003 12:26 GMT
> Hi there,
> I have some JTextField objects, called jtf0, jtf1, jtf2, and so on.
[quoted text clipped - 13 lines]
>
> ps: sorry for my bad english..

Hi Marco,

I think the best solution would be to use an array instead of dynamicly
accessing fields.

If you wish to access the fields of the current class you can do something
like this:

// to get/set a public/private property
this.getClass().getDeclaredField("fieldName").set(this, "newValue");
Object val = this.getClass().getDeclaredField("fieldName").get(this);

soo... in your case it would become something like this

for(int i=0;i<10;i++) {
  JTextField field = (JTextField) this.getClass().getDeclaredField("jtf" +
i).get(this);
  field.setText("hello " + i);
}

I don't know how to access temporary variables though. (In other words, the
above method works for properties of objects only, not variables declared
within a function)

Regards,
- Vincent


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.