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

public class ComponentDelegate<C extends JComponent,E> extends AbstractDelegate<C>
Instances of this are delegates for a specific components and events that are passed to user event action handlers (see 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 Details

    • ComponentDelegate

      public ComponentDelegate(C component, E event)
      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

      public final C 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

      public final void forComponent(Consumer<C> action)
      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

      public final E 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

      public final List<JComponent> 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

      public final void forSiblings(Consumer<List<JComponent>> action)
      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

      public 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. 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. Use forSiblingsOfType(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

      public final <T extends JComponent> void forSiblingsOfType(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. This method will make sure that the passed lambda will be executed by the Swing thread.

      Type Parameters:
      T - The JComponent 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

      public final List<JComponent> 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. Use forSiblinghood(Consumer) to access the sibling components
      Returns:
      A list of all siblings including the component from which this instance originated.
    • forSiblinghood

      public final void forSiblinghood(Consumer<List<JComponent>> action)
      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

      public 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. 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. Use forSiblinghoodOfType(Class, Consumer) to access the sibling components of the specified type in a thread-safe way.
      Type Parameters:
      T - The JComponent 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 - The JComponent 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.