Display a select widget.

Method Signatures and Parameters

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

label (String)

A short label explaining to the user what this 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 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 selectbox value changes. The value passed to the callback is the previous value of the component.

disabled(boolean disabled)

Disables the selectbox 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 selectbox 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.

placeholder(String placeholder)

Text displayed when no option is selected. Default varies based on widget configuration.

acceptNewOptions(boolean acceptNewOptions)

Whether the user can add a selection that isn't included in options. If this is false (default), the user can only select from the items in options. If this is true, the user can enter a new item that doesn't exist in options.

When a user enters a new item, it is returned by the widget as a string. The new item is not added to the widget's drop-down menu. Jeamlit will use a case-insensitive match from options before adding a new item.

Only compatible with selectbox of String values.

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 dropdown selection

 import tech.catheu.jeamlit.core.Jt;

 import java.util.List;

 public class SelectBoxApp {
     public static void main(String[] args) {
         String country = Jt.selectbox("Select your country",
                                       List.of("United States", "Canada", "United Kingdom", "Germany", "France")).use();

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

Dropdown with default value

 import tech.catheu.jeamlit.core.Jt;

 public class ProcessingSelectBoxApp {
     public static void main(String[] args) {
         String priority = Jt.selectbox("Task priority",
                                        List.of("Low", "Medium", "High", "Critical"))
                             .index(1)
                             .use();
         Jt.text("Priority: " + priority).use();
     }
 }

forum

Still have questions?

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