Class AsyncPanel


public abstract class AsyncPanel extends Div<AsyncPanel>
An abstract panel that performs its content modification asynchronously in a background thread.

AsyncPanel allows you to build UI content that requires time-consuming operations (such as database queries, API calls, or complex computations) without blocking the main page rendering. The panel automatically executes the createAsync() method in a background thread and sends updates to the client when complete.

Subclasses should override Element.create() to build initial placeholder content (such as skeleton loaders) and implement createAsync() for the real content. The async method is called automatically when the page loads or when the panel is added to an already-loaded page.

To prevent a brief flash of placeholder content when async work completes quickly, use setMinDisplayTime(long) to enforce a minimum display duration before the update is sent to the client.

Usage Example:


 public class DataPanel extends AsyncPanel {

     protected void create() {
         // Build skeleton placeholder content
         addElement(new LoadingSpinner());
     }

     protected void createAsync() {
         removeAllElements();
         List<Record> records = database.fetchRecords();
         for (Record record : records) {
             addElement(new Paragraph(record.getName()));
         }
     }
 }
 
Author:
Marvin P. Warble Jr.
See Also:
  • Constructor Details

    • AsyncPanel

      public AsyncPanel()
  • Method Details

    • setMinDisplayTime

      protected void setMinDisplayTime(long minDisplayTime)
      Sets the minimum time (in milliseconds) that placeholder content must remain visible before the async content replaces it. This prevents a distracting flash when async work completes faster than expected. A value of 0 (the default) disables the minimum display time.
      Parameters:
      minDisplayTime - Minimum display time in milliseconds.
    • createAsync

      protected abstract void createAsync()
      Called in a background thread to create the panel's real content.

      Implement this method to add elements, fetch data, or perform any operations needed to build the panel content. This method runs asynchronously, so it will not block page rendering. When this method completes, the panel automatically sends an update to the client.

    • onCreated

      protected void onCreated()
      Description copied from class: Element
      Hook method called after the element has been created.

      Override this method to perform actions after creation is complete.

      Overrides:
      onCreated in class Element<AsyncPanel>
    • onPageLoaded

      protected void onPageLoaded()
      Description copied from class: Element
      Hook method called when the page has finished loading.

      Override this method to perform actions after the page is fully loaded in the browser.

      Overrides:
      onPageLoaded in class Element<AsyncPanel>