0

I make an .exe file that just prints "Hello World 1" in a jlabel. Below there is a jtextfield in which user enters a number, "1" is default so it's printed "Hello World 1". Now, I want as user enters for example "2" then not only "Hello World 2" to be printed but as soon as it closes the .exe program and reopens its, "Hello World 2" to be the default text (the int to be the last the user entered). I mean, the string value of jtextfield to be perma saved. How can I do this? Thanks a lot

  • Simplest way is to write users data somewhere outside your application and try to read this data when application starts. You can write it to file you define, to some database, or maybe [properties](http://docs.oracle.com/javase/tutorial/essential/environment/properties.html). – Pshemo Sep 06 '14 at 14:51
  • Use `java.util.prefs`, example: http://stackoverflow.com/a/4017189/1774484 – Benjamin Albert Sep 06 '14 at 14:52
  • i've thought of that, read and write the int from a .txt file for example. but besides that? :/ –  Sep 06 '14 at 14:52

2 Answers2

1

Maybe use a Properties file. It allows your to store data in a file and access the data by a keyword.

So at startup you would check the file for your property. If it exits then you use the value in the text field. If it doesn't exist, then you set a default value for the field.

Then when you close the frame you will need to save the value back to the Properties file. Check out Closing An Application for more information on intercepting the close function of a frame.

camickr
  • 321,443
  • 19
  • 166
  • 288
1

Add WindowListener to listen the close event of your JFrame:

        myFrame.addWindowListener(new WindowAdapter() {
        public void windowClosed(WindowEvent evebt) {
            // Save the value of your JTextField now
            System.out.println("Bye!");
          }
        });
Audrius Meškauskas
  • 20,936
  • 12
  • 75
  • 93