Display a radio button widget.

Method Signatures and Parameters

Jt.radio(String label, List<T> options)

label (String)

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


options (List<T>)

The list of options to choose from

Chainable builder methods

index(Integer index)

The index of the preselected option on first render. If null, initializes empty and returns null until user selection. Defaults to 0 (the first option).

formatFunction(function.Function<T, String> formatFunction)

Function to modify the display of the radio options. The Function receives the raw option object and returns a String that will be used as display label. Does not impact the return value of the component.

help(String help)

A tooltip that gets displayed next to the widget label. If null, no tooltip is displayed.

onChange(function.Consumer<T> onChange)

An optional callback invoked when the radio button's value changes. The value passed to the callback is the previous value of the component.

disabled(boolean disabled)

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

horizontal(boolean horizontal)

Orients the radio group horizontally instead of vertically when set to true.

captions(List<String> captions)

A list of captions to show below each radio button. If null (default), no captions are shown. Must match the size of the options list.

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 radio group 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()

(Nullable T)

The current Nullable T value of the component.

Examples

Simple radio selection

 import tech.catheu.jeamlit.core.Jt;

 import java.util.List;

 public class RadioApp {
     public static void main(String[] args) {
         String size = Jt.radio("Select size",
             List.of("Small", "Medium", "Large")).use();

         if (size != null) {
             Jt.text("Selected size: " + size).use();
         }
     }
 }

Radio with custom objects

 import tech.catheu.jeamlit.core.Jt;

 public class ProductRadioApp {
     public static void main(String[] args) {
         record Product(String name, double price) {}

         Product selected = Jt
                 .radio("Choose product",
                        List.of(new Product("Basic Plan", 9.99),
                                new Product("Pro Plan", 19.99),
                                new Product("Enterprise Plan", 49.99)))
                 .formatFunction(e -> e.name + " ($" + e.price + ")")
                 .use();

         if (selected != null) {
             Jt.text("You chose: " + selected.name()).use();
             Jt.text("Price: $" + selected.price()).use();
         }
     }
 }

forum

Still have questions?

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