wood burning stoves 2.0*
The moose likes Swing / AWT / SWT and the fly likes How to set JTextField with the name in a String Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "How to set JTextField with the name in a String" Watch "How to set JTextField with the name in a String" New topic
Author

How to set JTextField with the name in a String

Jonathan Sullivan
Greenhorn

Joined: Mar 21, 2011
Posts: 11
Hello,
I'm a new comer in the Java Ranch and this is my first post.

So I would like to say hello to all forum people.

Then, I have a problem:

I have a JTextFiled name that is determinated at runtime:



jTextFiled1 or jTextField2 are already create in my swing application.

Then I would like to set this JTextField.
The name is in the String "campo".

The correct sintax is:



But in this way doesn't work:



Any suggestions?

Thank you in advance for your replies
Greg Reeder
Ranch Hand

Joined: Jun 14, 2011
Posts: 99

From what i see from your code "campo" is not the name of the JTextField, but rather the name of the string. If you need to change the String you could say campo = new String("string"); or campo = "string"; if you need to change the text of a text field, be sure that you are calling the correct thing.



Greg Reeder
Ranch Hand

Joined: Jun 14, 2011
Posts: 99

Oh, and welcome. I find this forum a fantastic place. The people are always helpful, never mean, and I have yet to have a bad experiance. Enjoy.
Jonathan Sullivan
Greenhorn

Joined: Mar 21, 2011
Posts: 11
Hello,
thank you for your reply.

campo is a String containg the name of the JTextfiled.

So i have:
jTextField1 = new javax.swing.JTextField();
jTextField2 = new javax.swing.JTextField();

String campo= "jTextField1";
or
String campo= "jTextField2";

I know I have to do:
jTextField1.setText("my String");
or
jTextField2.setText("my String");

But I would like to do:
campo.setText("my String")
because I don't know if I have to set jTextField1 or jTextField2.

I would like to have the name of JTextFiled I have to use in a variable.
Jonathan Sullivan
Greenhorn

Joined: Mar 21, 2011
Posts: 11
I can add some points.

I would like to do like in JavaScript:

<input name='myName1' type="text" value="default" />
<input name='myName2' type="text" value="default" />



or

Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4523
    
    5

If you think you need to do that, you have a bad design.

Ever heard of arrays?

And if your application really really requires you to associate a JTextField with a String, you would create that association in a Map<String, JTextField>.


luck, db
There are no new questions, but there may be new answers.
Greg Reeder
Ranch Hand

Joined: Jun 14, 2011
Posts: 99

I agree, You should either put these JTextFields in an array or even better, in a List. I would do the following:



that code is not meant to compile, but send you in the right direction. this is a much better way of going about accessing multiple Jtextfields. The arraylist has advantages over a standard array because you need not specify how big it needs to be upon creation.

Good luck.
Greg Reeder
Ranch Hand

Joined: Jun 14, 2011
Posts: 99

Oh, and the Map idea for the association between string and JTextField is also a fantastic idea; from what i see though, it is a frieght train to the pick up truck you really need.
Jonathan Sullivan
Greenhorn

Joined: Mar 21, 2011
Posts: 11
dear Greg and Darryl,

thank you for your answers.

The point is that I already know how to programming (in PHP win OO, Perl with OO, JavaScript),
but always with some dinamyc language.

My problem is how programming in a static language
and I have to learn the Java way to do stuff.

So I know what arrays are .

I will try your codes and I will do a feedback to you.

for now, thank you.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to set JTextField with the name in a String
 
Similar Threads
Blank Frame or Panel
[SOLVED] must implement the inherited abstract method
Identify a component that has been added by iteration
how to give focus to a component ?
using Double