Auto filling a jTextfield in java swing

故事扮演 提交于 2019-12-03 00:47:21

问题


I am creating a small system using swing via netbeans(I must say i am new to swing). look at the below image .

in this the id = "ST100" i need to add it to a jTextfield automatically in the below interface when i open it

so i coded it in the first interface source code as seen below after making the arrowed textfield's access modifier "public" which is in the second interface.

but the wanted text is not auto filling why is it?

the relevent code in the first interface

addSubjects add = new addSubjects();
add.stid1.setText(stdid);

"stdid" is the String input of the 1st interface text field. "addSubjects" is the frame name of the 2nd interface. "stid1" is the variable name of the arrowed text field in the 2nd interface.

there are no codes in the second interface. what i want is to after clicking the submit button in the first interface the arrowed text field in the 2nd interface must autofill the same String which i give to the first interaface arrowed textfield.


回答1:


So, you want to pass the String value from one class to another.

Well, the easiest and (best) way to achieve this is by creating the 2nd class as an inner class of the first class.

  • All you have to do is to Paste your 2nd class code inside your 1st class code (Don't paste it inside the main method)

  • Remove the static word from your 2nd class's main method

  • Declare a String in your 1st class as follows,

    String StudentId;

  • After you collect the Id from the user, intialize the above mentioned String as,

    String StudentId = "student_id_to_show";

  • Now,in your 2nd class code(which is inner class of the 1st class), set the text of the jTextField as,

    jTextField.setText(StudentId);

All done,Cheers!!!



来源:https://stackoverflow.com/questions/50890382/auto-filling-a-jtextfield-in-java-swing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!