Class JScrollPanels

All Implemented Interfaces:
ImageObserver, MenuContainer, Serializable, Accessible, ScrollPaneConstants, StylableComponent

public class JScrollPanels extends UI.ScrollPane
The JScrollPanels class is a container for a list of scrollable UI components representing view models or simple data models which are dynamically turned into views by a ViewSupplier. This class exists to compensate for the deficits of the JList and JTable components, whose entries are not able to receive user events like for example mouse events, button clicks etc...
A JScrollPanels instance can arrange its entries in a vertical or horizontal manner based on the UI.Align parameter.

Instances of this store view model implementations in a view model property list so that they can dynamically be turned into views by a ViewSupplier lambda when the list changes its state.
Here a simple example demonstrating the usage of the JScrollPanels class through the Swing-Tree API:

    UI.scrollPanels()
    .add(viewModel.entries(), entry ->
        UI.panel().add(UI.button("Click me! :)"))
    )
 	
...where entries() is a method returning a Vars instance which contains a list of your sub-view models. The second parameter of the UIForAnySwing.add(sprouts.Vals, ViewSupplier) method is a lambda which takes a single view model from the list of view models and turns it into a view.
See Also:
  • Method Details

    • of

      public static JScrollPanels of(UI.Align align, @Nullable Dimension size)
      Constructs a new JScrollPanels instance with the provided alignment and size.
      Parameters:
      align - The alignment of the entries inside this JScrollPanels instance. The alignment can be either UI.Align.HORIZONTAL or UI.Align.VERTICAL.
      size - The size of the entries in this JScrollPanels instance.
      Returns:
      A new JScrollPanels instance.
    • getNumberOfEntries

      public int getNumberOfEntries()
      Allows you to get the number of entries which are currently managed by this JScrollPanels. The number of entries is the number of view models which are currently managed by this JScrollPanels.
      Returns:
      The number of entries which are currently managed by this JScrollPanels.
    • addEntry

      public <M extends EntryViewModel> void addEntry(M entryViewModel, ViewSupplier<M> viewSupplier)
      The JScrollPanels does not store components statically in the UI tree. Instead, it is a hybrid of the traditional static approach and a renderer based approach (as in the JList). The lambda passed to this method is responsible for continuously supplying a UI which fits a certain context (which defines if the entry is selected or not among other things).
      Type Parameters:
      M - The type of the entry view model.
      Parameters:
      entryViewModel - A view model which ought to be added.
      viewSupplier - A provider lambda which ought to turn a context object into a fitting UI.
    • addEntry

      public <M extends EntryViewModel> void addEntry(String constraints, M entryViewModel, ViewSupplier<M> viewSupplier)
      The JScrollPanels does not store components statically in the UI tree. Instead, it is a hybrid of the traditional static approach and a renderer based approach (as in the JList). The view supplier lambda passed to this method is responsible for continuously supplying a UI which fits a certain context (which defines if the entry is selected or not among other things).
      Type Parameters:
      M - The type of the entry view model.
      Parameters:
      constraints - The constraints which ought to be applied to the entry.
      entryViewModel - The entry model which ought to be added.
      viewSupplier - A provider lambda which ought to turn a context object into a fitting UI.
    • addAllEntries

      public <M extends EntryViewModel> void addAllEntries(@Nullable String constraints, List<M> entryViewModels, ViewSupplier<M> viewSupplier)
      Adds multiple entries at once to this JScrollPanels.
      Type Parameters:
      M - The type of the entry view model.
      Parameters:
      constraints - The constraints which ought to be applied to the entry.
      entryViewModels - A list of entry models which ought to be added.
      viewSupplier - A provider lambda which ought to turn a context object into a fitting UI.
    • removeAllEntries

      public void removeAllEntries()
      Use this to remove all entries.
    • removeEntryAt

      public void removeEntryAt(int index)
      Use this to remove an entry at a certain index.
      Parameters:
      index - The index of the entry which ought to be removed.
    • addEntryAt

      public <M extends EntryViewModel> void addEntryAt(int index, @Nullable String attr, M entryViewModel, ViewSupplier<M> viewSupplier)
      Use this to add an entry at a certain index.
      Type Parameters:
      M - The type of the entry view model.
      Parameters:
      index - The index at which the entry ought to be added.
      attr - The constraints which ought to be applied to the entry, may be null.
      entryViewModel - The entry view model which ought to be added.
      viewSupplier - The supplier which is used to create the view for the given entry view model.
    • setEntryAt

      public <M extends EntryViewModel> void setEntryAt(int index, @Nullable String attr, M entryViewModel, ViewSupplier<M> viewSupplier)
      Use this to replace an entry at a certain index.
      Note: This method will replace an existing entry at the given index.
      Type Parameters:
      M - The type of the entry view model.
      Parameters:
      index - The index at which the entry ought to be placed.
      attr - The constraints which ought to be applied to the entry, may be null.
      entryViewModel - The entry view model which ought to be added.
      viewSupplier - The supplier which is used to create the view for the given entry view model.
    • getSelected

      public <T extends JComponent> Optional<T> getSelected(Class<T> type)
      Use this to find an entry component.
      Type Parameters:
      T - The component type which ought to be found.
      Parameters:
      type - The component type which ought to be found.
      Returns:
      The found entry panel matching the provided type class and predicate lambda.
    • forEachEntry

      public void forEachEntry(Consumer<JScrollPanels.EntryPanel> action)
      Use this to iterate over all panel list entries.
      Parameters:
      action - The action which ought to be applied to all JScrollPanels entries.
    • forEachEntry

      public <T extends JComponent> void forEachEntry(Class<T> type, Consumer<JScrollPanels.EntryPanel> action)
      Use this to iterate over all panel list entries of a certain type by supplying a type class and a consumer action. Neither of the two parameters may be null!
      Type Parameters:
      T - The entry value type parameter.
      Parameters:
      type - The type of the entry which ought to be iterated over.
      action - The action which ought to be applied to all JScrollPanels entries of the given type.
    • setSelectedFor

      public <T extends JComponent> void setSelectedFor(Class<T> type, Predicate<T> condition)
      Use this to set entries as selected based on a condition lambda (predicate).
      Type Parameters:
      T - The type of the entry which ought to be selected.
      Parameters:
      type - The type of the entry which ought to be selected.
      condition - The condition which ought to be met for the entry to be selected.