Display a toggle widget.

Method Signatures and Parameters

Jt.toggle(String label)

label (String)

A short label explaining to the user what this toggle is for. Markdown is supported, see Jt#markdown(String) for more details.

Chainable builder methods

value(boolean value)

Preselect the toggle when first rendered.

onChange(function.Consumer<Boolean> onChange)

An optional callback function invoked when the toggle value changes. The value passed in the callback is the previous value of the component.

help(String help)

A tooltip that gets displayed next to the text. If this is null (default), no tooltip is displayed.

disabled(boolean disabled)

Disables the toggle if set to true. When disabled, users cannot interact with the widget.

labelVisibility(JtComponent.LabelVisibility labelVisibility)

The visibility of the label. The default is VISIBLE. If this is HIDDEN, Jeamlit displays an empty spacer instead of the label, which can help keep the widget aligned with other widgets. If this is COLLAPSED, Jeamlit displays no label or spacer.

width(String width)

The width of the element. This can be one of the following:

  • content (default): The width of the element matches the width of its content, but doesn't exceed the width of the parent container.
  • stretch: The width of the element matches the width of the parent container.
  • An integer specifying the width in pixels: The element has a fixed width. If the specified width is greater than the width of the parent container, the width of the element matches the width of the parent container.

width(int widthPixels)

The width of the element in pixels. The element will have a fixed width. If the specified width is greater than the width of the parent container, the width of the element matches the width of the parent container.

key(String key)

A string to use as the unique key for the widget. If this is omitted, a key will be generated for the widget based on its content. No two widgets may have the same key.

No description

use()

Put the widget in the app, in the MAIN container.

use(JtContainer container)

Put the widget in the app, in the provided container.

Returns after .use()

(NotNull Boolean)

The current NotNull Boolean value of the component.

Examples

Simple toggle

 import tech.catheu.jeamlit.core.Jt;

 public class ToggleApp {
     public static void main(String[] args) {
         boolean enabled = Jt.toggle("Enable notifications").use();

         Jt.text("Notifications: " + (enabled ? "Enabled" : "Disabled")).use();
     }
 }

Toggle with default value

 import tech.catheu.jeamlit.core.Jt;

 public class ToggleDefaultApp {
     public static void main(String[] args) {
         boolean autoSave = Jt.toggle("Auto-save")
             .value(true)
             .use();

         if (autoSave) {
             Jt.text("Changes will be saved automatically").use();
         }
     }
 }

forum

Still have questions?

Go to our discussions forum for helpful information and advice from Jeamlit experts.