Class ComponentDelegate<C extends JComponent,E>
- Type Parameters:
C- The delegate (in most cases origin UI component) type parameter stored by this.E- The event type parameter of the event stored by this.
- Direct Known Subclasses:
ComponentMouseEventDelegate
Action),
with the purpose of providing useful context information to the action handler.
You would typically use this to access and change the state of the component, schedule animations for the component or query the tree of neighboring components.
Here a nice usage example where the delegate is used to animate a button:
button("I turn green when you hover over me")
.onMouseEnter( it ->
it.animateFor(0.5, TimeUnit.SECONDS, status -> {
double highlight = 1 - status.progress() * 0.5;
it.setBackgroundColor(highlight, 1, highlight);
})
)
.onMouseExit( it ->
it.animateFor(0.5, TimeUnit.SECONDS, status -> {
double highlight = 0.5 + status.progress() * 0.5;
it.setBackgroundColor(highlight, 1, highlight);
})
)
In this example the it parameter is a ComponentDelegate<JButton,MouseEvent>
which can be used to access/modify the button, the event, the sibling components...
...but also exposes a nice API to schedule animations for the button.
For some more examples please take a look at the living swing-tree documentation where you can browse a large collection of examples demonstrating how to use the API of this class.
-
Constructor Summary
ConstructorsConstructorDescriptionComponentDelegate(C component, E event) Creates a new delegate for the specified component and event. -
Method Summary
Modifier and TypeMethodDescriptionfinal voidforComponent(Consumer<C> action) Provides a thread-safe way to access the underlyingJComponentof this delegate by passing aConsumerlambda which will always be executed on the GUI thread (the AWT Event Dispatch Thread).final voidforSiblinghood(Consumer<sprouts.Tuple<JComponent>> action) Provides a thread-safe way to access the entire siblinghood of this delegate (the delegated component and its siblings) by passing aConsumerlambda which will always be executed on the GUI thread (the AWT Event Dispatch Thread).final <T extends JComponent>
voidforSiblinghoodOfType(Class<T> type, Consumer<sprouts.Tuple<T>> action) Provides a thread-safe way to access the entire siblinghood of this delegate (including the delegated component itself) filtered by the given type, by passing aConsumerlambda which will always be executed on the GUI thread (the AWT Event Dispatch Thread).final voidforSiblings(Consumer<sprouts.Tuple<JComponent>> action) Provides a thread-safe way to access the sibling components of this delegate by passing aConsumerlambda which will always be executed on the GUI thread (the AWT Event Dispatch Thread).final <T extends JComponent>
voidforSiblingsOfType(Class<T> type, Consumer<List<T>> action) Provides a thread-safe way to access the sibling components of this delegate filtered by the given type by passing aConsumerlambda which will always be executed on the GUI thread (the AWT Event Dispatch Thread).final CExposes the underlyingJComponentfrom which this delegate and user event actions originate.final EgetEvent()Exposes the event that represents the action that was triggered either by the user or by the system.final sprouts.Tuple<JComponent> This method provides a convenient way to access all the children of the parent component of the component this delegate is for, including the delegated component itself.final <T extends JComponent>
sprouts.Tuple<T> getSiblinghoodOfType(Class<T> type) Allows you to query the entire siblinghood of the delegated component filtered by the specified type.final sprouts.Tuple<JComponent> Exposes the "siblings", which consist of all the child components of the parent of the delegated component, except for the delegated component itself.final <T extends JComponent>
List<T> getSiblingsOfType(Class<T> type) Allows you to query the sibling components of the delegated component of the specified type.Methods inherited from class swingtree.AbstractDelegate
_component, _isUndefinedColor, _isUndefinedFont, _siblingsSource, animateFor, animateFor, animateFor, animateFor, animateStyleFor, animateStyleFor, find, find, find, findAll, findAllByGroup, findAllByGroup, findAllByGroup, findAllByGroup, get, getBackground, getBorder, getBounds, getCursor, getFont, getForeground, getHeight, getLocation, getMaxSize, getMinSize, getParent, getPrefSize, getSize, getTooltip, getWidth, getX, getY, isEnabled, isOpaque, isVisible, paint, paint, paint, paint, parentDelegate, setBackground, setBackgroundColor, setBackgroundColor, setBackgroundColor, setBackgroundColor, setBorder, setBounds, setBounds, setBounds, setCursor, setCursor, setEnabled, setFont, setForeground, setForegroundColor, setForegroundColor, setForegroundColor, setForegroundColor, setHeight, setLocation, setMaxHeight, setMaxSize, setMaxSize, setMaxSize, setMaxWidth, setMinHeight, setMinSize, setMinSize, setMinSize, setMinWidth, setPrefHeight, setPrefSize, setPrefSize, setPrefSize, setPrefWidth, setSize, setSize, setSize, setTooltip, setVisible, setWidth, shapeOf, style
-
Constructor Details
-
ComponentDelegate
Creates a new delegate for the specified component and event.- Parameters:
component- The component for which the delegate is created.event- The event that represents the action that was triggered either by the user or by the system.
-
-
Method Details
-
getComponent
Exposes the underlyingJComponentfrom which this delegate and user event actions originate.Threading: A
JComponentis owned by the GUI thread (the AWT Event Dispatch Thread). Under a non-coupled threading model (seeEventProcessor), event actions may be invoked on the application thread rather than on the EDT. To prevent unsafe access to Swing state, this method requires the calling thread to be the GUI thread and throws anIllegalStateExceptionotherwise.If you need to access the component from the application thread, use
forComponent(Consumer)instead, which will safely dispatch the supplied lambda to the GUI thread for you.- Returns:
- The component for which the current
Actionoriginated. - Throws:
IllegalStateException- If this method is called from a non-Swing thread.- See Also:
-
forComponent
Provides a thread-safe way to access the underlyingJComponentof this delegate by passing aConsumerlambda which will always be executed on the GUI thread (the AWT Event Dispatch Thread).This is the recommended counterpart to
getComponent()when the calling code may run on the application thread (e.g. under a decoupledEventProcessor). If the current thread is already the GUI thread, the lambda is executed immediately; otherwise it is dispatched viaUI.run(Runnable).Example:
it.forComponent( button -> button.setText("Updated on the EDT") );- Parameters:
action- The action consuming the component, which will be executed by the Swing thread.- See Also:
-
getEvent
Exposes the event that represents the action that was triggered either by the user or by the system.- Returns:
- An object holding relevant information about an event that was triggered.
-
getSiblings
Exposes the "siblings", which consist of all the child components of the parent of the delegated component, except for the delegated component itself.Threading: Sibling components are part of the Swing component tree, which is owned by the GUI thread (the AWT Event Dispatch Thread). This method requires the calling thread to be the GUI thread and throws an
IllegalStateExceptionotherwise. Under a non-coupledEventProcessor, event actions are usually invoked on the application thread, in which case you must useforSiblings(Consumer)instead, which dispatches the supplied lambda to the GUI thread for you.- Returns:
- A tuple (immutable list) of all siblings excluding the component from which this instance originated.
- Throws:
IllegalStateException- If this method is called from a non-Swing thread.- See Also:
-
forSiblings
Provides a thread-safe way to access the sibling components of this delegate by passing aConsumerlambda which will always be executed on the GUI thread (the AWT Event Dispatch Thread).This is the recommended counterpart to
getSiblings()when the calling code may run on the application thread (e.g. under a decoupledEventProcessor). If the current thread is already the GUI thread, the lambda is executed immediately; otherwise it is dispatched viaUI.run(Runnable).- Parameters:
action- The action consuming a list of all siblings (excluding the component from which this instance originated), which will be executed by the Swing thread.- See Also:
-
getSiblingsOfType
Allows you to query the sibling components of the delegated component of the specified type. So a list of all siblings which are of the specified type will be returned, excluding the currently delegated component itself.Threading: Sibling components are part of the Swing component tree, which is owned by the GUI thread (the AWT Event Dispatch Thread). This method requires the calling thread to be the GUI thread and throws an
IllegalStateExceptionotherwise. Under a non-coupledEventProcessor, event actions are usually invoked on the application thread, in which case you must useforSiblingsOfType(Class, Consumer)instead, which dispatches the supplied lambda to the GUI thread for you.- Type Parameters:
T- The type of the sibling components to return.- Parameters:
type- The type class of the sibling components to return.- Returns:
- A list of all siblings of the specified type, excluding the component from which this instance originated.
- Throws:
IllegalStateException- If this method is called from a non-Swing thread.- See Also:
-
forSiblingsOfType
Provides a thread-safe way to access the sibling components of this delegate filtered by the given type by passing aConsumerlambda which will always be executed on the GUI thread (the AWT Event Dispatch Thread).This is the recommended counterpart to
getSiblingsOfType(Class)when the calling code may run on the application thread (e.g. under a decoupledEventProcessor). If the current thread is already the GUI thread, the lambda is executed immediately; otherwise it is dispatched viaUI.run(Runnable).- Type Parameters:
T- TheJComponenttype of the sibling components to return.- Parameters:
type- The type class of the sibling components to return.action- The action consuming a list of all siblings of the specified type, excluding the component from which this instance originated, which will be executed by the Swing thread.- See Also:
-
getSiblinghood
This method provides a convenient way to access all the children of the parent component of the component this delegate is for, including the delegated component itself.Threading: Sibling components are part of the Swing component tree, which is owned by the GUI thread (the AWT Event Dispatch Thread). This method requires the calling thread to be the GUI thread and throws an
IllegalStateExceptionotherwise. Under a non-coupledEventProcessor, event actions are usually invoked on the application thread, in which case you must useforSiblinghood(Consumer)instead, which dispatches the supplied lambda to the GUI thread for you.- Returns:
- A tuple (immutable list) of all siblings including the component from which this instance originated.
- Throws:
IllegalStateException- If this method is called from a non-Swing thread.- See Also:
-
forSiblinghood
Provides a thread-safe way to access the entire siblinghood of this delegate (the delegated component and its siblings) by passing aConsumerlambda which will always be executed on the GUI thread (the AWT Event Dispatch Thread).This is the recommended counterpart to
getSiblinghood()when the calling code may run on the application thread (e.g. under a decoupledEventProcessor). If the current thread is already the GUI thread, the lambda is executed immediately; otherwise it is dispatched viaUI.run(Runnable).- Parameters:
action- The action consuming a list of all siblings (including the component from which this instance originated), which will be executed by the Swing thread.- See Also:
-
getSiblinghoodOfType
Allows you to query the entire siblinghood of the delegated component filtered by the specified type. So a list of all siblings which are of the specified type will be returned, possibly including the delegated component itself.Threading: Sibling components are part of the Swing component tree, which is owned by the GUI thread (the AWT Event Dispatch Thread). This method requires the calling thread to be the GUI thread and throws an
IllegalStateExceptionotherwise. Under a non-coupledEventProcessor, event actions are usually invoked on the application thread, in which case you must useforSiblinghoodOfType(Class, Consumer)instead, which dispatches the supplied lambda to the GUI thread for you.- Type Parameters:
T- TheJComponenttype of the sibling components to return.- Parameters:
type- The type of the sibling components to return.- Returns:
- A list of all siblings of the specified type, including the component from which this instance originated.
- Throws:
IllegalStateException- If this method is called from a non-Swing thread.- See Also:
-
forSiblinghoodOfType
public final <T extends JComponent> void forSiblinghoodOfType(Class<T> type, Consumer<sprouts.Tuple<T>> action) Provides a thread-safe way to access the entire siblinghood of this delegate (including the delegated component itself) filtered by the given type, by passing aConsumerlambda which will always be executed on the GUI thread (the AWT Event Dispatch Thread).This is the recommended counterpart to
getSiblinghoodOfType(Class)when the calling code may run on the application thread (e.g. under a decoupledEventProcessor). If the current thread is already the GUI thread, the lambda is executed immediately; otherwise it is dispatched viaUI.run(Runnable).- Type Parameters:
T- TheJComponenttype of the sibling components to return.- Parameters:
type- The type of the sibling components to return.action- The action consuming a tuple (immutable list) of all siblings of the specified type, including the component from which this instance originated, which will be executed by the Swing thread.- See Also:
-