• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Using the Preferences API and Interfaces and Constants

 
Andrew Monkhouse
author and jackaroo
Marshal Commander
Pie
Posts: 12014
220
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For those who dont subscribe, this months Sun Core Technology Tech Tips has two articles that may be of interest to SCJD developers:
  • USING THE PREFERENCES API.
    There are many times when you need to persist configuration information for an application. In the past, you might have used java.util.Properties to save details about the preferred date format for an application, the preferred window size or location, or the previous time that a user logged into an application. The Properties class required you to write to and read from streams. This, in turn, meant that you had to manage where the properties files were stored in your file system. In other words, much of your effort was spent managing details that had little to do with the information being managed.
    The Preferences API introduced in J2SE 1.4 provides a convenient framework for storing and recalling data about user-specific preferences or system-wide preferences. In this tip, you'll learn how to use the Preferences API to store and recall user-specific preferences.
  • INTERFACES AND CONSTANTS.
    A discussion on using interfaces to create global constants in your application - why this is bad, and what Java 1.5 will offer that is better.
    [ July 15, 2003: Message edited by: Andrew Monkhouse ]
  •  
    Philippe Maquet
    Bartender
    Posts: 1872
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks Andrew, that's very interesting stuff ! I'll consider using the Preferences API, especially because as I needed some listening mechanism to property changes, I was a bit reinventng the wheel... which I've heard graders hate !
    Cheers,
    Phil.
     
    Vlad Rabkin
    Ranch Hand
    Posts: 555
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hi Andrew,
    Thanx a lot. It seems I can make some things with it better!
    Vlad
     
    Philippe Maquet
    Bartender
    Posts: 1872
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hi Andrew, Vlad,
    It was too nice, and I have to change my mind about what I just wrote.
    My instructions state :
    All configuration must be done via a GUI, and must be persistent between runs of the program. Such configuration information must be stored in a file called suncertify.properties which must be located in the current working directory.

    Well, Preferences uses a backing store which is implementation-specific, so it seems that we must reinvent the wheel ...
    Regards,
    Phil.
     
    Majid AlMeshari
    Greenhorn
    Posts: 2
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hi Phillip,
    Still, u dont have to reinvent the wheel.
    Try "java.util.Properties". very simple to use and persist between the runs of the application. This is an excerpt from the class description Sun has provided:
    "The Properties class represents a persistent set of properties. The Properties can be saved to a stream or loaded from a stream. Each key and its corresponding value in the property list is a string."
    Your single property should look like this:
    property = value.
    Remember that everything is a String, so u should cast in case you are using numbers.
    Good luck..
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic