how to store font settings
I have a program using a certain font setting for several panels in a JFrame and Dialogs, etc. I'd like to keep these settings separate somewhere and provide a possibility (a menu/dialog..something) to change e.g. font.
What might be a good design to organize things like a used font, stored somewhere and if changed it should change in several locations?
[369 byte] By [
Corcovadoa] at [2007-10-2 20:21:35]

In Swing terms, the font has become part of the model for the component rather than a view property.
Therefore I'd create a model to represent this state (making it Observable in the way that models should be). For example:
public class ViewStateModel {
private Font font;
private PropertyChangeSupport changeSupport = new SwingPropertyChangeSupport(this);
public Font getFont() {
return font;
}
public void setFont(Font font) {
Font oldFont = this.font;
this.font = font;
changeSupport.firePropertyChange("font", oldFont, this.font);
}
public void addPropertyChangeListener(PropertyChangeListener listener) {
changeSupport.addPropertyChangeListener(listener);
}
public void removePropertyChangeListener(PropertyChangeListener listener) {
changeSupport.removePropertyChangeListener(listener);
}
}
I've not bothered to write custom listener/event interfaces as the Javabean standard should be sufficient for this.
All your components that depend on this state should be customised to accept this model and listen for changes to it. For example:
public class MySpecialButton extends JButton implements PropertyChangeListener {
public MySpecialButton(Action action, ViewStateModel model) {
super(action);
model.addPropertyChangeListener(this);
// TODO - provide mechanism for removing this listener when the button's no longer needed
updateViewState(model);
}
public void propertyChange(PropertyChangeEvent e) {
ViewStateModel model = (ViewStateModel)e.getSource();
updateViewState(model);
}
protected void updateViewState(ViewStateModel model) {
setFont(model.getFont());
}
}
Then, to change the font all you need to do is provide a dialog that calls setFont on the model - all the registered listening components will automatically update.
Hope this helps.
Wow! I really didn't expect to get something else than blurry hints to start more readings.. But not on any account I expected to get an out of the box solution! I appreciate a lot, many thanx!!!
Although I haven't tested it so far. I'll post again, if I encounter any problems - By now it looks exactly like what I'm looking for.
PS. I used a Observable in another application a while ago, but didn't really know if an observable is the approach of choice for such kind of problem or rather messing things up.