Class ProgressBar

All Implemented Interfaces:
PanelElement

public class ProgressBar extends AbstractPanelElement<ProgressBar>
A filled bar indicating progress on a 0-to-1 scale. The "bounded-progress indicator" primitive of the component library.

Works in all three rendering contexts. Render-only element.

Two forms for the progress value:

  • Fixed value — pass a float directly. Unusual; useful for static-progress decorative bars.
  • Supplier-driven value — pass a DoubleSupplier returning a normalized 0.0–1.0 value for progress that changes over time (the common case). This is the same canonical numeric-supplier shape that Slider and ScrollContainer read their normalized values from, so a Slider's double value feeds a ProgressBar with no box-and-cast: ProgressBar.spec(w, h, () -> s.value).

Configuration fixed at construction: fill direction (ProgressBar.Direction), fill color, background color, and optional label.

Clamping

Values outside [0, 1] are clamped silently. A value of 1.5f renders as a full bar; a value of -0.5f renders as empty. No exception, no warning. This is deliberate — progress computations sometimes legitimately overshoot (a timer tick briefly exceeding duration before reset), and exceptions on progress values would be noisy. Consumers debugging unexpected display can rely on this documented behavior.

Label positioning

If a label is supplied, it renders centered on the bar's 2D bounds — horizontal-center and vertical-center of the bar rectangle. Vertical bars still render the label on the same 2D center (not rotated). Consumers wanting a label above or below a vertical bar position a separate TextLabel alongside the bar.

Rendering

Solid-color fills via GuiGraphicsExtractor.fill(). No textures; no sprite. Consumers wanting themed sprite-backed bars implement PanelElement directly.

Scope

  • No animation — value changes render immediately per frame.
  • No multi-segment bars — consumers compose multiple ProgressBars for segmented displays.
  • No percentage formatting — label supplier returns literal text.
See Also:
  • Field Details

    • DEFAULT_FILL_COLOR

      public static final int DEFAULT_FILL_COLOR
      Default fill color — white.
      See Also:
    • DEFAULT_BG_COLOR

      public static final int DEFAULT_BG_COLOR
      Default background color — dark gray.
      See Also:
    • DEFAULT_DIRECTION

      public static final ProgressBar.Direction DEFAULT_DIRECTION
      Default direction — left-to-right.
  • Constructor Details

    • ProgressBar

      public ProgressBar(int childX, int childY, int width, int height, float value)
      Creates a ProgressBar with a fixed value, left-to-right direction, default colors, and no label.
    • ProgressBar

      public ProgressBar(int childX, int childY, int width, int height, float value, ProgressBar.Direction direction, int fillColor, int bgColor, @Nullable Supplier<net.minecraft.network.chat.Component> label)
      Creates a ProgressBar with a fixed value and full configuration.
      Parameters:
      childX - X position within panel content area
      childY - Y position within panel content area
      width - bar width in pixels
      height - bar height in pixels
      value - progress value (clamped to [0, 1])
      direction - fill direction
      fillColor - ARGB fill color (must include alpha byte)
      bgColor - ARGB background color (must include alpha byte)
      label - optional label supplier; null for no label
    • ProgressBar

      public ProgressBar(int childX, int childY, int width, int height, DoubleSupplier value)
      Creates a ProgressBar with a supplier-driven value, left-to-right direction, default colors, and no label.

      value is a DoubleSupplier returning a normalized 0.0–1.0 progress — the same canonical numeric-supplier shape Slider and ScrollContainer use, so a double-valued source feeds this bar with no box-and-cast.

    • ProgressBar

      public ProgressBar(int childX, int childY, int width, int height, DoubleSupplier value, ProgressBar.Direction direction, int fillColor, int bgColor, @Nullable Supplier<net.minecraft.network.chat.Component> label)
      Creates a ProgressBar with a supplier-driven value and full configuration.
      Parameters:
      childX - X position within panel content area
      childY - Y position within panel content area
      width - bar width in pixels
      height - bar height in pixels
      value - normalized progress supplier (DoubleSupplier, value in 0..1); invoked each frame; result clamped to [0, 1]
      direction - fill direction
      fillColor - ARGB fill color (must include alpha byte)
      bgColor - ARGB background color (must include alpha byte)
      label - optional label supplier; null for no label
  • Method Details

    • self

      protected ProgressBar self()
      Description copied from class: AbstractPanelElement
      Returns this typed as the concrete subclass. Each subclass implements this as return this;. The base's chainable setters route their return through this hook so they hand back the concrete type instead of AbstractPanelElement, keeping fluent chains typed end-to-end.
      Specified by:
      self in class AbstractPanelElement<ProgressBar>
    • spec

      public static ElementSpec spec(int width, int height, DoubleSupplier value)
      Returns an ElementSpec for a default-styled progress bar (left-to-right, default colors, no label).

      value is a DoubleSupplier returning a normalized 0.0–1.0 progress — the canonical numeric-supplier shape — so a Slider's double value feeds this with no cast: ProgressBar.spec(w, h, () -> s.slider).

    • spec

      public static ElementSpec spec(int width, int height, DoubleSupplier value, ProgressBar.Direction direction, int fillColor, int bgColor, @Nullable Supplier<net.minecraft.network.chat.Component> label)
      Layout spec with full configuration.
    • getWidth

      public int getWidth()
      Description copied from interface: PanelElement
      Width in pixels.
    • getHeight

      public int getHeight()
      Description copied from interface: PanelElement
      Height in pixels.
    • fillWidth

      public void fillWidth(int width)
      Column-fill (Pass 3): stretch this bar to the column's widest extent.
      Parameters:
      width - the column's widest-child extent to stretch to, in pixels
    • naturalWidth

      public int naturalWidth()
      Natural (authored) width before any panel constraint.
    • layoutWithin

      public void layoutWithin(int budget)
      Cap the bar to the panel's budget so it never bleeds; reversible.
      Parameters:
      budget - horizontal pixels available to this element, in panel content space. Always >= 1.
    • render

      public void render(RenderContext ctx)
      Description copied from interface: PanelElement
      Renders this element. Called during the panel background pass (screen space), after slot backgrounds.

      Position the element using the context's content origin plus this element's childX/childY:

      int sx = ctx.originX() + getChildX();
      int sy = ctx.originY() + getChildY();
      
      Parameters:
      ctx - per-frame render context
    • size

      public ProgressBar size(int width, int height)
      Fluent resize sugar — sets the bar's pixel dimensions and returns this bar for chaining. Additive to the positional constructors.
    • getCurrentValue

      public float getCurrentValue()
      Returns the current progress value, clamped to [0, 1]. Resolves the supplier.
    • getDirection

      public ProgressBar.Direction getDirection()
      Returns the fill direction.
    • getFillColor

      public int getFillColor()
      Returns the ARGB fill color.
    • getBgColor

      public int getBgColor()
      Returns the ARGB background color.