how to get String in jFrame and pass to another jFrame? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

how to get String in jFrame and pass to another jFrame?

I have two jFrame (LoginForm, MainForm), LoginForm have a TextField (tfusername), how can get tfusername.gettext() in MainForm?

8th Sep 2017, 8:37 PM
hamid
hamid - avatar
6 Answers
+ 2
What returns null? And can you post the whole code?
8th Sep 2017, 9:41 PM
ChaoticDawg
ChaoticDawg - avatar
+ 1
Whoops, guess I misunderstood what you were asking. You can pass the text variable via an instance method of the Jframe you are passing it to.
8th Sep 2017, 8:51 PM
ChaoticDawg
ChaoticDawg - avatar
+ 1
a simple way: in loginform>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> public class LoginForm extends javax.swing.JFrame{ public static String Username; public static String Password; //create a method public void InsertData(){ Username = tfusername.gettext(); Password = tfpassword.gettext(); } in MainForm>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> String user = UserName; String Pass = Password;
8th Sep 2017, 10:07 PM
hamid
hamid - avatar
0
You can just use the getText() method of the JTextField class. String text = tfusername.getText(); https://docs.oracle.com/javase/7/docs/api/javax/swing/JTextField.html Note that it is inherited from JTextComponent https://docs.oracle.com/javase/7/docs/api/javax/swing/text/JTextComponent.html
8th Sep 2017, 8:44 PM
ChaoticDawg
ChaoticDawg - avatar
0
return null
8th Sep 2017, 9:39 PM
hamid
hamid - avatar