1

My Idea is to give the User a chance to store a value (in my case an int how broad the JFrame should be, so that the next time he starts my .jar, I can make it that broad from beginning on and he hasn't got to resize it again).

As it is only one single value, I would not like to have to write it into a separate file etc., so is there an elegant way, sth. like a variable more "hard code" than usual?

And Yes, I used the Searchbar of my browser, but I had no Idea what to search for...

msrd0
  • 7,816
  • 9
  • 47
  • 82
  • the term you should use is *persist* or *persistence*. It means to save something beyond the application runtime. *Hard Code* is definitly not what you mean. It means to store something inside your source code (like a constant). – Waog Aug 07 '14 at 20:38
  • Even though you don't want to write it as a separate file, since this is a user preference, that's usually the recommended way to do it. – Paul Wostenberg Aug 07 '14 at 20:38
  • You need to store it in a file. The preferences API is one way of doing this. – Peter Lawrey Aug 07 '14 at 20:42

3 Answers3

4

The Preferences class does this. You should not use it for more application specific values though. The size and location of the windows and such is what it is used for.

Preferences prefs = Preferences.userRoot();
prefs.put("AppWindowSize", "10");
prefs.get("AppWindowSize","DefaultValueHere");
Xabster
  • 3,710
  • 15
  • 21
3

You would probably benefit from storing your information using the Preferences API.

See http://docs.oracle.com/javase/8/docs/technotes/guides/preferences/index.html for instructions.

Thorbjørn Ravn Andersen
  • 73,784
  • 33
  • 194
  • 347
-1

Can you do it? Yes. You can put a properties file in your classpath and then use getResourceAsStream to load it from a package.

Should you do it? No. Consider the case where the user makes a mistake and sets their JFrame size to 10000000. They won't be able to fix that unless they unpack your JAR, change the properties file, then repackage it. It's better to use a separate file.

Community
  • 1
  • 1
Brad
  • 2,261
  • 3
  • 22
  • 32