File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to set JTextField with the name in a String

 
Jonathan Sullivan
Greenhorn
Posts: 11
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 99
Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 99
Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5115
11
Java Netbeans IDE Opera
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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>.
 
Greg Reeder
Ranch Hand
Posts: 99
Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 99
Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic