Package swingtree
Class ComponentDelegate<C extends JComponent,E>
java.lang.Object
swingtree.AbstractDelegate<C>
swingtree.ComponentDelegate<C,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
Instances of this are delegates for a specific components and events that
are passed to user event action handlers (see
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:
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 void
forComponent
(Consumer<C> action) Use this to access the component of this delegate in the swing thread.final void
forSiblinghood
(Consumer<List<JComponent>> action) Use this to access the sibling components of this delegate in the swing thread.final <T extends JComponent>
voidforSiblinghoodOfType
(Class<T> type, Consumer<List<T>> action) Use this to access all sibling components (including the one represented by this delegate) of the specified type in the swing thread.final void
forSiblings
(Consumer<List<JComponent>> action) Use this to access the sibling components of this delegate in the swing thread.final <T extends JComponent>
voidforSiblingsOfType
(Class<T> type, Consumer<List<T>> action) Use this to access the sibling components of this delegate of the specified type in the swing thread.final C
Exposes the underlying component from which this delegate and user event actions originate.final E
getEvent()
Exposes the event that represents the action that was triggered either by the user or by the system.final List
<JComponent> This method provides a convenient way to access all the children of the parent component of the component this delegate is for.final <T extends JComponent>
List<T> getSiblinghoodOfType
(Class<T> type) Allows you to query the sibling components of the delegated component of the specified type.final List
<JComponent> Exposes the "siblings", which consist of all the child components of the parent of the delegated component except the 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, getScale, getSize, getTooltip, getWidth, getX, getY, isEnabled, isOpaque, isVisible, paint, paint, paint, paint, parentDelegate, scale, scale, scale, scale, setBackground, setBackgroundColor, setBackgroundColor, setBackgroundColor, setBackgroundColor, setBorder, setBounds, setBounds, setBounds, setCursor, setCursor, setEnabled, setFont, setForeground, setForegroundColor, setForegroundColor, setForegroundColor, setForegroundColor, setHeight, setMaxHeight, setMaxSize, setMaxSize, setMaxSize, setMaxWidth, setMinHeight, setMinSize, setMinSize, setMinSize, setMinWidth, setOpaque, 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 underlying component from which this delegate and user event actions originate. This method may only be called by the Swing thread. If another thread calls this method, an exception will be thrown.- Returns:
- The component for which the current
Action
originated. - Throws:
IllegalStateException
- If this method is called from a non-Swing thread.
-
forComponent
Use this to access the component of this delegate in the swing thread. This method will make sure that the passed lambda will be executed by the Swing thread.- Parameters:
action
- The action consuming the component, which will be executed by the Swing thread.
-
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 the for the delegated component itself.- Returns:
- A list of all siblings excluding the component from which this instance originated.
-
forSiblings
Use this to access the sibling components of this delegate in the swing thread. This method will make sure that the passed lambda will be executed by the Swing thread.- 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.
-
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.
Note that this method may only be called by the Swing thread. If another thread calls this method, an exception will be thrown. UseforSiblingsOfType(Class, Consumer)
to access the sibling components of the specified type in a thread-safe way.- 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.
-
forSiblingsOfType
Use this to access the sibling components of this delegate of the specified type in the swing thread. This method will make sure that the passed lambda will be executed by the Swing thread.- Type Parameters:
T
- TheJComponent
type 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.
-
getSiblinghood
This method provides a convenient way to access all the children of the parent component of the component this delegate is for. Note that this method may only be called by the Swing thread. If another thread calls this method, an exception will be thrown. UseforSiblinghood(Consumer)
to access the sibling components- Returns:
- A list of all siblings including the component from which this instance originated.
-
forSiblinghood
Use this to access the sibling components of this delegate in the swing thread. This method will make sure that the passed lambda will be executed by the Swing thread.- 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.
-
getSiblinghoodOfType
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, possibly including the delegated component itself.
Note that this method may only be called by the Swing thread. If another thread calls this method, an exception will be thrown. UseforSiblinghoodOfType(Class, Consumer)
to access the sibling components of the specified type in a thread-safe way.- Type Parameters:
T
- TheJComponent
type 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.
-
forSiblinghoodOfType
public final <T extends JComponent> void forSiblinghoodOfType(Class<T> type, Consumer<List<T>> action) Use this to access all sibling components (including the one represented by this delegate) of the specified type in the swing thread. This method will make sure that the passed lambda will be executed by the Swing thread.- Type Parameters:
T
- TheJComponent
type of the sibling components to return.- Parameters:
type
- The type of the sibling components to return.action
- The action consuming a list of all siblings of the specified type, including the component from which this instance originated, which will be executed by the Swing thread.
-