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.
What you will build
Section titled “What you will build”Before you start
Section titled “Before you start”- Stores and their scopes: Stores and remembered properties.
- Controls and their signal binding: Controls.
- Skins for the controls: this guide uses the plain checkbox and slider that Build a UI kit writes in its first step.
1. Write the store
Section titled “1. Write 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.
2. Bind the controls to the store
Section titled “2. Bind the controls to the store”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()inonChangewrites 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.
If it does not work
Section titled “If it does not work”useStorethrowsIllegalStateException: The store … has no @UIStoreData annotation: add the annotation to the store class.- The settings come back to their defaults after a restart: the scope is not
PERMANENT, orfromJsonreads other keys thantoJsonwrites. - The error output prints
[JOID] The store … cannot be restored from …:fromJsonthrew on the file, such as aClassCastExceptionon a number cast toInteger; JOID keeps the file aside and starts the store from its defaults. - 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). - 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.
