2

I have used Delphi for more than 10 years, and am now interested in using Eclipse and Java. With Delphi, I have developed code whereby with any application, regardless of how many forms, text boxes, checkboxes and other means of setting user preferences, I can save and load the defaults with one line of code to save and one to load in any new application I develop.

With a Java gui, I have managed to programmatically gather a list of all the user interface gui components, but have not managed to use ObjectInputStream to load the settings saved with ObjectOutputStream. There are two problems (so far!):

  1. ObjectInputStream.ReadObject() needs typecasting to the class being loaded. Is there a way to do this when the class is not known until runtime?
  2. ObjectInputStream.ReadObject() appears to produce a new instance of an oject, and I do not know how to read the data into the existing components (JTextField(s) etc).

Yes, I'll admit I am a newbie! Thanks for any help.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Red wine
  • 162
  • 2
  • 11
  • 2
    Why not `java.util.Preferences`? – trashgod Oct 23 '12 at 03:56
  • 3
    It seems for this the best approach is not to serialize components like text areas & check boxes, but values like string & boolean. I would recommend a `Properties` file if the choices can be presented that way, or XML (as one of a number of alternatives) otherwise. Or, as mentioned by @trashgod - `Preferences` (so many alternatives, including ones specific to [Java Web Start](http://stackoverflow.com/tags/java-web-start/info)!) – Andrew Thompson Oct 23 '12 at 03:56
  • I thought with serialization it might be possible to avoid the need to write code to save the required properties of each type of gui component. – Red wine Oct 23 '12 at 04:01
  • Object serialization is not meant for long term storage, from the docs *Serialized objects of this class will not be compatible with future Swing releases. The current serialization support is appropriate for short term storage or RMI between applications running the same version of Swing. As of 1.4, support for long term storage of all JavaBeansTM has been added to the java.beans package. Please see XMLEncoder.*, take a look at [Long Term Persistence](http://docs.oracle.com/javase/tutorial/javabeans/advanced/longpersistence.html) – MadProgrammer Oct 23 '12 at 04:08
  • Direct object serialization is not successful (or necessary) for most of the GUI related classes of interest. (And Java is not Delphi.) – Andrew Thompson Oct 23 '12 at 04:09
  • Thanks so much for your prompt suggestions. I would be happy to use XML encoder/Decoder if I could work out how to read into an existing gui object. XMLDecoder appears to create a new instance of an object. – Red wine Oct 23 '12 at 04:35
  • *"XMLDecoder appears to create a new instance of an object."* Why is that a problem? Also, add @PersonName to ensure someone is notified of a new comment. – Andrew Thompson Oct 23 '12 at 04:48
  • I'm trying to read the saved parameters of the gui components back into the gui components on the forms (JPanel's). I use the Window.getWindows() and Container.getComponents() methods to get an ArrayList of all gui components in the application. I can save these with XMLEncoder, but I can't work out how to use XMLDecoder to get the information back into the components in this array list, since XMLDecoder creates a new instance of the component rather than reading into an existing component. Thanks again for helping a newbie. @MadProgrammer – Red wine Oct 23 '12 at 05:35
  • @user1767050 In that case, you might be more interested in `JAXB` (or Java XML Bindings), which will allow you to read write object properties to and from XML. This would require you to have a staging object with the properties you want saved/loaded, but the basic concept is sound, take a look at [Java Architecture for XML Binding](http://docs.oracle.com/javase/tutorial/jaxb/index.html). You might also find [XStream](http://xstream.codehaus.org/tutorial.html) of use. Failing that, you may need to use the `Preferences` API as suggested by trashgod. – MadProgrammer Oct 23 '12 at 05:47
  • @user1767050: Re `Preferences`, see also this [answer](http://stackoverflow.com/a/11724866/230513) _ff_. – trashgod Oct 23 '12 at 13:30

1 Answers1

1

I would highly recommend you check Java Preferences or simply the java Properties class which is simpler.

Waleed Almadanat
  • 1,027
  • 10
  • 24