Skip to main content

Listeners

Configuration change events can be listened for using the following methods:

ConfigaddListener(ConfigEventListener)removeListener(ConfigEventListener)

The ConfigEventListener interface consists of the following method:

ConfigEventListeneronConfigChange(ConfigEvents)

The ConfigEvent class provides access to all the information about the updated entry:

ConfigEventgetType()getSection()getKey()getValue()getModifiers()getComment()getPreLines

The listener method is triggered:

  • After Config.commit() is called.
  • When the file changes on the file system.

In both cases, the listener is triggered after the changes have been committed.

final Config config = Config.create("MyConfig.cfg").build();

// Add a listener for changes to MySection/key1
config.addListener(
new ConfigEventListener() {
@Override
public void onConfigChange(ConfigEvents events) {
for (ConfigEvent event : events) {
if (event.getType() == SET_ENTRY) {

String section = event.getSection();
String key = event.getKey();

if (section.equals("MySection") && key.equals("key1")) {

// Get the new value from the event.
String newVal = event.getValue();

// Or get the new value from the config (since the change has already been committed).
newVal = config.getString("MySection/key1");
}
}
}
}
}
)