PopupMenu

@Composable
fun PopupMenu(anchor: PopupAnchor, expanded: Boolean, onDismiss: () -> Unit, content: @Composable () -> Unit)

A menu the application opens itself - a drop-down button, an overflow menu, a shortcut - shown over the component anchor is bound to.

The menu is open while expanded is true. Closing is the toolkit's own doing and cannot be refused: adopt the close onDismiss reports by setting expanded back to false, or the next true declares nothing new and the menu stays shut.

val anchor = rememberPopupAnchor()
var expanded by remember { mutableStateOf(false) }
Button("File", onClick = { expanded = true }, modifier = SwingModifier.popupAnchor(anchor))
PopupMenu(anchor, expanded, onDismiss = { expanded = false }) {
MenuItem("Open", onClick = { })
}

The menu's items are a composition of their own, nested in this one, so they see the same state and androidx.compose.runtime.CompositionLocals as this call. They are composed when the menu opens and released when it closes, and a menu on screen follows the state its items read.

A menu declared open against an anchor that is not bound yet - or bound to a component not yet on screen - waits and opens once there is somewhere to anchor to.

For the menu a right-click opens, use ContextMenu.

Parameters

anchor

the handle naming the component the menu opens over, bound with popupAnchor.

expanded

whether the menu is open.

onDismiss

invoked when the user closes the menu.

content

the composable menu tree shown in the menu.

See also


@Composable
fun PopupMenu(anchor: PopupAnchor, expanded: Boolean, display: (popup: JPopupMenu, invoker: Component, x: Int, y: Int) -> Unit, onDismiss: () -> Unit, content: @Composable () -> Unit)

Variant of PopupMenu that lets the caller decide how the populated JPopupMenu is presented, instead of the default of showing it at the anchor point under the bound component.

Parameters

anchor

the handle naming the component the menu opens over.

expanded

whether the menu is open.

display

invoked with the populated popup, the anchored component, and the anchor point (x, y in that component's coordinates) to present the popup.

onDismiss

invoked when the user closes the menu.

content

the composable menu tree shown in the menu.