Skip to main content

JXSplitPane!

No replies
rbair
Offline
Joined: 2003-07-08

Earlier this evening I filed issue # 63 (https://jdnc.dev.java.net/issues/show_bug.cgi?id=63), which is in regards to saving gui user preferences (for instance, the location of the divider in a JSplitPane).

I'm proposing a JXSplitPane class that would extend JSplitPane and contain code for automatically saving/retrieving the divider location, per the code in issue #63. Example JXSplitPane:

/**
* This special split pane queries the Preferences API to determine where to
* locate the divider, and saves divider locations whenever they change to preferences.
* @author Richard Bair
*/
public class JXSplitPane extends JSplitPane {
/**
* The key in the preferences for this setting
*/
private String preferencesKey = "JXSplitPane";
/**
* True if the split pane is being initialized. This is used to keep
* the pane from setting the preference while it is initializing the
* split pane.
*/
private boolean initializing;

/**
*
*/
public JXSplitPane() {
super();
init();
}
/**
* @param newOrientation
*/
public JXSplitPane(int newOrientation) {
super(newOrientation);
init();
}
/**
* @param newOrientation
* @param newContinuousLayout
*/
public JXSplitPane(int newOrientation, boolean newContinuousLayout) {
super(newOrientation, newContinuousLayout);
init();
}
/**
* @param newOrientation
* @param newLeftComponent
* @param newRightComponent
*/
public JXSplitPane(int newOrientation, Component newLeftComponent, Component newRightComponent) {
super(newOrientation, newLeftComponent, newRightComponent);
init();
}
/**
* @param newOrientation
* @param newContinuousLayout
* @param newLeftComponent
* @param newRightComponent
*/
public JXSplitPane(int newOrientation, boolean newContinuousLayout, Component newLeftComponent, Component newRightComponent) {
super(newOrientation, newContinuousLayout, newLeftComponent, newRightComponent);
init();
}

/**
* Initializes the split pane. This gets the divider location from Preferences and sets it. If the
* divider location is not specified in Preferences, then this will set the divider to its
* default location.
*/
private void init() {
initializing = true;
//read from the prefs
setDividerLocation(Prefs.getNodeFromUserRoot(preferencesKey).getInt("divider_location", getDividerLocation()));
initializing = false;
}

/**
* Saves the divider location to prefs whenever the location is changed (except when the location
* is changed during initialization).
* @inheritDoc
*/
public void setDividerLocation(int location) {
if (!initializing) {
//save to the prefs
Prefs.getNodeFromUserRoot(preferencesKey).putInt("divider_location", getDividerLocation());
}
super.setDividerLocation(location);
}
/**
* @return Returns the preferencesKey.
*/
public String getPreferencesKey() {
return preferencesKey;
}
/**
* @param preferencesKey The preferencesKey to set.
*/
public void setPreferencesKey(String preferencesKey) {
this.preferencesKey = preferencesKey;
init();
}
}