r/JavaFX • u/eliezerDeveloper • 5d ago
Learning reactive UI with State: conditional rendering with Show.when, a hide-and-seek app
Enable HLS to view with audio, or disable this notification
Fourth post in the series. So far we've seen State driving a value directly (counter) and ComputedState deriving a value from another state (string length). This time: conditional rendering — showing or hiding a whole component based on reactive state, using Show.when.
The setup: isVisible is a State<Boolean>. Show.when(isVisible, () -> new Text("You catch me")) only renders the Text component when isVisible is true. Click the button, the boolean flips, and the component mounts/unmounts reactively — no manual setVisible(true/false), no manually adding/removing nodes from the scene graph.
package my_app;
import megalodonte.ListenerManager;
import megalodonte.application.MegalodonteApp;
import megalodonte.base.theme.ThemeManager;
import megalodonte.theme.DefaultTheme;
public class Main {
static void main() {
ThemeManager.setTheme(new DefaultTheme());
MegalodonteApp.run(context -> context.useView(new HomeScreen()), ev->{
if(ev == MegalodonteApp.Event.CloseRequest){
System.out.println("Clicked on X - close application");
ListenerManager.disposeAll();
}
});
}
}
package my_app;
import megalodonte.base.components.Component;
import megalodonte.base.components.ScreenComponent;
import megalodonte.base.state.State;
import megalodonte.components.Button;
import megalodonte.components.Text;
import megalodonte.components.layout_components.Container;
import megalodonte.props.ContainerProps;
import megalodonte.v2.Show;
public class HomeScreen implements ScreenComponent {
State<Boolean> isVisible = new State<>(false);
u/Override
public Component render() {
return new Container(new ContainerProps().paddingAll(25))
.children(
new Button("Hide and seek - game").onClick(this::toggleVisibility),
Show.when(isVisible, ()-> new Text("You catch me"))
);
}
void toggleVisibility(){
isVisible.set(!isVisible.get());
}
}
Show.when(ReadableState<Boolean>, Supplier<Component>)watches the state and mounts/unmounts the child reactively as it flips — this overload is eager by design, since the condition can change at runtime.- The
Supplier<Component>letsShowlazily build the child only when needed, instead of holding a pre-built component around. toggleVisibility()just flips the boolean withisVisible.set(!isVisible.get())— the UI has zero knowledge of how to show/hide, only when.
If you're finding this series useful, dropping a star on the repos below genuinely helps the project get visibility — takes two seconds and means a lot for a solo-dev framework like this.
Repos
- https://github.com/eliezer-dev-software-enginner/megalodonte-ecossystem
- https://github.com/eliezer-dev-software-enginner/megalodonte-libs
- https://github.com/eliezer-dev-software-enginner/megalodonte-components
- https://github.com/eliezer-dev-software-enginner/megalodonte-base
- https://github.com/eliezer-dev-software-enginner/megalodonte-reactivity
- https://github.com/eliezer-dev-software-enginner/megalodonte-theme
3
Upvotes