setContent

fun Container.setContent(parent: CompositionContext? = null, content: @Composable () -> Unit): DisposableHandle

Sets the composable content of any Container (a JPanel, a window's content pane, ...).

With no parent the content joins the composition the container's own place in the Swing tree resolves to: an enclosing composition when the container is nested under one, otherwise the composition shared by the owning top-level Window, so every content composition in one window recomposes together. A container detached from any window is not an error: the content is mounted as soon as the container is attached to a window, and disposing the returned handle before that happens mounts nothing. A container in a window that has been disposed composes where it stands, on a composition that lasts as long as the handle returned here.

A parent drives the composition instead, and the content composes on this call, whatever the container is attached to - which is what reaches a container that is built to be read rather than shown. Everything mounted inside this content joins parent as well: a setContent naming no parent of its own on a container hanging under this one resolves to parent rather than to the composition its window shares. The caller owns what they pass: disposing the returned handle disposes this content composition and leaves parent running. A container that later ends up in a different window joins the composition of the window it is then in, recreating this content there - unless parent is a recomposer of the caller's own, which the content is kept on. A move then brings only the window the content reads up to date, and everything the content remembered survives it.

The content reads its LocalWindow from the composition it joins, and the window this container is in wherever that composition names none.

The content also reads a LifecycleOwner as its LocalLifecycleOwner, shared with everything this content hosts - a popup, a menu, an overlay. It is the owner the composition this content joins carries, so an owner provided over that composition - by a caller, or by a navigation library - reaches this content too. Failing that it is the owner of composed content this container hangs under, a window's own content as a rule. Content that finds neither is given an owner of its own following this container, and only such an owner is ended at DESTROYED by disposing the returned handle. A Window or Dialog composed in this content is a top-level window of its own and states an owner of its own, whatever stands over the declaration.

An owner reports RESUMED while the content it follows is in a window showing at full size that holds the keyboard focus, STARTED while that content is shown without the focus, and CREATED while it has no native peer - detached, or in a window Swing has not realized yet - or while its window is minimized. It answers for the content it was given to follow, so content reading an owner it did not get for itself reports where that window's content stands rather than where this container hangs. Since RESUMED is the focused state, repeatOnLifecycle(RESUMED) ends its work the moment the user switches to another window - work that should run for as long as the content is on screen belongs under STARTED.

Must be called on the Event Dispatch Thread.

Return

a DisposableHandle that disposes this content composition when invoked (or cancels it if it has not mounted yet). Must be disposed on the Event Dispatch Thread.

Parameters

parent

the composition context this content joins and shares the recomposition scope of, and the one anything mounted inside this content joins. Defaults to null, meaning the composition the container's own place in the Swing tree resolves to.

content

the composable content to set


fun Window.setContent(content: @Composable () -> Unit): DisposableHandle

Sets the composable content of a Window (a javax.swing.JFrame, javax.swing.JDialog, or javax.swing.JWindow).

The content is hosted on the window's content pane and joins the composition shared across that window.

Must be called on the Event Dispatch Thread.

Return

a DisposableHandle that disposes the composition when invoked. Must be disposed on the Event Dispatch Thread.

Parameters

content

the composable content to set


fun JMenuBar.setContent(content: @Composable () -> Unit): DisposableHandle

Sets the composable content of a JMenuBar.

Joins a composition like Container.setContent: the enclosing composition when nested, otherwise the composition shared by the owning window. A menu bar installed on a window shares that window's composition.

A menu bar is routinely built before it is installed on its frame (bar.setContent { ... } then frame.jMenuBar = bar), so at the call site it usually has no window ancestor yet. As with Container.setContent, that is not an error: the content is mounted the moment the menu bar gains a window ancestor (when it is installed on the frame) - and the menu tree reads that window as its LocalWindow, so an item's callback reaches the window its menu hangs off. Must be called on the Event Dispatch Thread.

Return

a DisposableHandle that disposes this menu-bar composition (or cancels it if it has not mounted yet). Must be disposed on the Event Dispatch Thread.

Parameters

content

the composable menu tree (Menu, MenuItem, ...)