views:

134

answers:

1

I have been developing desktop application in Java. Here, it shows how i reach string of Properties file. Key-value of String is HDI.Device.1.ID

org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(HDIManagementApp.class).getContext().getResourceMap(HDIManagementView.class);String ID=resourceMap.getString("HDI.Device.1.ID");

Problem is: I don't want to call that String by-value. I want to call it by reference. Because, at the same time, I want to use Listeners for that object. If "HDI.Device.1.ID" value updates, then I will do something. `

    //After changes "mystr", we inform the table model about new value

     MyString mystr = new yaziyorumartik.data.MyString();
     mystr.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
        public void propertyChange(PropertyChangeEvent evt) {
            Object object=evt.getNewValue();
            tableModel.setValueAt(object.toString(), 0, 5);
            throw new UnsupportedOperationException("Not supported yet.");
        }
    });`

mystr.setValue(ID)

When mystr HDI.Device.1.ID in properties file updates then mystr doesn't update.

A: 

Resource files are intended for localization. Thus values of all properties are loaded eagerly at initialization of the ResourceBundle object (which is wrapped by the ResourceMap). Values are not changed in memory when you modify the resource file after the application is started. You shouldn't need to listen for changes when the ResourceMap is used for localization.

If you need to manage configuration (that can be changed during runtime), you should use other classes / libraries depending on framework you use, but I doubt you'll find one that monitors property file for changes. The common way this is handled is as follows:

  • you have one property file for default values of your configuration props
  • you provide an UI / programmatic interface for changing the property values at runtime
  • the actual properties are stored in yet another file which is re-created on every runtime property change (if this file does not yet exist - e.g. after first start or after reseting the configuration to defaults), you just initialize it with defaults
schmeedy
OK. I had already done interface. All i need is how can i store actual properties on every runtime property change?
Iguramu
It depends on how the change in your property (or preference to be more acurrate) was initiated - when you provide a form where user can edit preferences, there often is a button called "Apply" which triggers transfer of values from the form into an object used by your application as an working representation of actual preferences. Furthermore, a change in actual preferences should trigger storing the new preferences to a file. Initiation of this action can be either a part of the "Apply" button handler or you can attach it as a listener for some PreferenceChangeEvent or so...
schmeedy