Basically I've got a text editor which revolves around a jTextArea. There are some settings (e.g. whether line wrap is enabled) that I'd like to save to a file when the application closes, and then load up again when it runs next.

I've spent a few hours trying to get it to work by saving to a plain text file and failed, then tried using a serialisable class (which probably isn't even meant to work that way) - and have eventually given up on getting it working without help :)

There are two main questions I have:

1. How should I implement the whole process of saving the settings before I quit, and loading them up again when the program starts up next? Is a plain text file the best solution?

The main problem I had earlier with this was with retrieving the information. I was able to save the settings to the file properly, but kept getting NullReferenceErrors when trying to read values back in. I worked out that the file was being found successfully and opened, but the data was being read in as 'null' for some reason.

2. What is the best way to let the user modify these settings?

I've been trying to implement it using a modal JDialog. Is that the best way, or would you suggest something else?

Keep in mind that I'm doing this to teach myself the language. So please don't just give me answers :) I'd rather just be pointed in the right direction if possible.


Recommended Answers

All 2 Replies

Java has a Preferences class that's ideal for what you need. It's as easy as

Preferences prefs = Preferences.userRoot().node("myEditor");
prefs.putBoolean("lineWrap", true);
if (prefs.getBoolean("lineWrap", false)) ...

UI should be a dialog, and keeping it modal makes coding, and using, it easier.

Thanks! If that supports objects as well as primitive data types it'll be perfect... though I could always work around that. Am looking up the API on it now.


And it doesn't seem to support objects (in particular I wanted to store a File object) - but storing the filepath as a string will probably be enough.

Thanks again! :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.