• Post Reply Bookmark Topic Watch Topic
  • New Topic

Write the value of a JTextField to a variable of type String from another class.  RSS feed

 
Marvin Legend
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am very difficult to record the value typed into a JTextField that belongs to class 1 in variable of type String that belongs to class 2, for example, if I type the word "Test" in the JTextField class 1 when i press the button in class 1, I would like the value entered in the JTextField class 1 is recorded on a variable type String in class 2, so I use this variable as I want, can you give me a hand with that? I've tried to create an object of class 1 and use the getText and to String, but no success yet. When i see the System in the class2 the result is NULL ! Thank you my friends !

The code :




Thank you Friends !
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Please go back and use the button to change the indentation of your code. It is not correctly indented, and the many unnecessary empty lines make it very difficult to read. The only thing I can read is public fields. Public fields are almost always incorrect. Names like Class1 are very poor and make the code harder to understand.
 
Bin Smith
Ranch Hand
Posts: 514
1
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello!

You have method returnField that returns value typed into fieldOfClass1.
 
Marvin Legend
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Volodymyr Levytskyi wrote:Hello!

You have method returnField that returns value typed into fieldOfClass1.


Hello my friend, thanks for the reply, but you see that when I start the Class1 I assign the value of the method into value variable of Class2? I want the text inserted into field entered in the field of class 1 is inserted in the variable of Class2. Thanks bro!
 
Bin Smith
Ranch Hand
Posts: 514
1
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you press button bottonOfClass1 ActionEvent is generated and registered listeners are called.
Your listener calls bottonActionPerformed method which assigns value of TextField fieldOfClass1 to classTwo.receiveFieldValueOfClass1.

I see you have what you want or I do not understand what you want
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your problem is because you are creating an new Class2 object and in it's constructor you are printing out your message and the value of receiveFieldValueOfClass1 (which is null as it hasn;t been set yet). Once the constructor completes your next line of code sets the value of receiveFieldValueOfClass1 (but the message has already been printed).

You can either pass the value into the constructor so it is available where you print out the message or have a setReceiveFieldValueOfClass1 method to pass the value into and put the print statement in there.

BTW Why does your Class2 class a) not have sensible name, b) have main method & c) have a public field called receiveFieldValueOfClass1?
 
Marvin Legend
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks to all !
Have a nice day my friends !
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!