简体   繁体   中英

Java and jtextfield value

I have jTextFields . If the user type tHe AET , the HOST and the PORT NUMBER in them, I need to save that permanently when he clicks on the save button. How can I do that with Java to display the values automatically every time I run my application?

You have many options.

You could save to a file .

PrintWriter pw = new PrintWriter(new File('myFile.txt'));
pw.printLine(textField.getText());

to read from file would be....

try (BufferedReader br = new BufferedReader(new FileReader(file))) {
    String line;
    while ((line = br.readLine()) != null) {
       // process the line.
    }
}

More information on files here

you could save to the users registry .

Preferences userPref = Preferences.userRoot();
userPref.put('textFieldKey', textField.getText());

to read from registry would be....

Preferences userPref = Preferences.userRoot();
userPref.get('textFieldKey', 'defaultValue');

More information on preferences/registry storage here

You could also save to a DB but this would require SQL knowledge.

Example and tut here

There is also the option of using serialization please see an example here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM