ComponentPropertyDescriptor
One Swing property of one or more component types. Define it once, in a top-level val, and declare it on a component with SwingModifier.property; see that function for the full contract.
Build a property of a single component type with the single-type invoke overload:
private val OpaqueProperty =
ComponentPropertyDescriptor<JComponent, Boolean>(
name = "opaque",
read = { it.isOpaque },
write = { c, v -> c.isOpaque = v }
)Build one that several unrelated types each declare for themselves - icon, which JLabel and AbstractButton each declare on their own, with no shared supertype that does - from ComponentPropertyAccessors built with accessor, one per type:
private val IconProperty =
ComponentPropertyDescriptor(
"icon",
accessor<JLabel, Icon?>(
read = { it.icon }, write = { c, v -> c.icon = v }
),
accessor<AbstractButton, Icon?>(
read = { it.icon }, write = { c, v -> c.icon = v }
),
)name is this property's whole identity: see SwingModifier.property for what that means across declarations and across handles.
The primary constructor is public intentionally for callers who need to provide a custom supported-type predicate. Prefer the companion builders when they express the property: they derive the runtime type information and keep the accessors together. When using this constructor directly, targetType must be compatible with T, handles must return true exactly for component types whose instances read and write can safely handle, read must return the current property value, and write must apply the supplied value to that property. The constructor's name is the stable property identity; targetType identifies the represented component type, read and write access its value, and handles reports which component types the descriptor supports.
Constructors
Types
The two ways to build a ComponentPropertyDescriptor: for one component type, or for several.
One component type's accessors for a property that several unrelated types declare separately, built with accessor.