Skip to content

Build a settings screen

Settings outlive the screen that edits them: the user expects to find them again after a restart. This guide keeps two settings, a sound switch and a volume, in a store saved on disk, and binds the controls of a screen to it.

Each control reads and writes a signal of the store.

Start from the UIStore of Keep a store on disk, with a BooleanSignal for the sound. This guide names its file audio in UIStoreData:

@UIStoreData(id = "audio", scope = UIStoreScope.PERMANENT)
public class SettingsGuideStore extends UIStore {
private final IntegerSignal volume = new IntegerSignal(80);
private final BooleanSignal sound = new BooleanSignal(true);

Full file: SettingsGuideStore.java

Check: the class compiles.

In init(), store is the store that useStore returns (Use a store from several UIs). Bind each control to its signal: checked(...) for the checkbox, value(...) for the slider. The control then shows the signal and writes the value the user picks into it:

TextNode.create(700, 400).text(Text.create("Sound", info)).anchorY(Align.CENTER).attach(this);
PlainGuideCheckboxNode
.create(1170, 375, 50, 50)
.checked(store.getSound())
.onChange(event -> store.save())
.attach(this);

Full file: SettingsGuideUI.java

TextNode
.create(700, 500)
.text(store.getVolume().map(volume -> Text.create("Volume " + volume, info)))
.anchorY(Align.CENTER)
.attach(this);
PlainGuideSliderNode
.create(920, 475, 300, 50)
.range(0, 100)
.value(store.getVolume())
.onChange(event -> store.save())
.enabled(store.getSound())
.attach(this);

Full file: SettingsGuideUI.java

  • save() in onChange writes the file as soon as a value changes, so a crash loses nothing.
  • enabled(store.getSound()) greys the slider out while the sound is off.
  • The label derives its text from the volume with map(...) (Derived values).

Check: move the volume to 30, then uncheck the sound: the label reads Volume 30, and the slider greys out. Close the application and start it again: the screen shows the same values, read from store/audio.store in the data folder.

  1. useStore throws IllegalStateException: The store … has no @UIStoreData annotation: add the annotation to the store class.
  2. The settings come back to their defaults after a restart: the scope is not PERMANENT, or fromJson reads other keys than toJson writes.
  3. The error output prints [JOID] The store … cannot be restored from …: fromJson threw on the file, such as a ClassCastException on a number cast to Integer; JOID keeps the file aside and starts the store from its defaults.
  4. The settings are lost when the application starts from another folder: the data folder is relative to the working directory by default; move it with dataDir(...) (Where JOID stores files).
  5. A change of a control reaches neither the store nor the other UIs: the control received a value, such as value(80), instead of the signal.

Report a problem with this page