Class ProgressBar
- All Implemented Interfaces:
PanelElement
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
floatdirectly. Unusual; useful for static-progress decorative bars. - Supplier-driven value — pass a
DoubleSupplierreturning a normalized0.0–1.0value for progress that changes over time (the common case). This is the same canonical numeric-supplier shape thatSliderandScrollContainerread their normalized values from, so aSlider'sdoublevalue feeds aProgressBarwith 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 of1.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 separateTextLabel alongside the bar.
Rendering
Solid-color fills viaGuiGraphicsExtractor.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:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enumFill direction for the progress bar. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intDefault background color — dark gray.static final ProgressBar.DirectionDefault direction — left-to-right.static final intDefault fill color — white.Fields inherited from class AbstractPanelElement
childX, childY -
Constructor Summary
ConstructorsConstructorDescriptionProgressBar(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(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.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.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. -
Method Summary
Modifier and TypeMethodDescriptionvoidfillWidth(int width) Column-fill (Pass 3): stretch this bar to the column's widest extent.intReturns the ARGB background color.floatReturns the current progress value, clamped to [0, 1].Returns the fill direction.intReturns the ARGB fill color.intHeight in pixels.intgetWidth()Width in pixels.voidlayoutWithin(int budget) Cap the bar to the panel's budget so it never bleeds; reversible.intNatural (authored) width before any panel constraint.voidrender(RenderContext ctx) Renders this element.protected ProgressBarself()Returnsthistyped as the concrete subclass.size(int width, int height) Fluent resize sugar — sets the bar's pixel dimensions and returns this bar for chaining.static ElementSpecspec(int width, int height, DoubleSupplier value) Returns anElementSpecfor a default-styled progress bar (left-to-right, default colors, no label).static ElementSpecspec(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.Methods inherited from class AbstractPanelElement
at, declId, getChildX, getChildY, getElementDeclId, getTooltipSupplier, isElementOpaque, isVisible, setChildPosition, setElementOpaque, setVisible, showWhen, tooltip, tooltip, tooltipSupplierMethods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface PanelElement
extraLayoutHeight, getActiveOverlayBounds, hitTest, isHovered, isInteractive, keyPressed, mouseClicked, mouseReleased, mouseScrolled, notifyClickOutsideOverlay, onAttach, onDetach, queueTooltip, renderOverlay
-
Field Details
-
DEFAULT_FILL_COLOR
public static final int DEFAULT_FILL_COLORDefault fill color — white.- See Also:
-
DEFAULT_BG_COLOR
public static final int DEFAULT_BG_COLORDefault background color — dark gray.- See Also:
-
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
Creates a ProgressBar with a supplier-driven value, left-to-right direction, default colors, and no label.valueis aDoubleSupplierreturning a normalized0.0–1.0progress — the same canonical numeric-supplier shapeSliderandScrollContaineruse, so adouble-valued source feeds this bar with no box-and-cast.
-
-
Method Details
-
self
Description copied from class:AbstractPanelElementReturnsthistyped as the concrete subclass. Each subclass implements this asreturn this;. The base's chainable setters route their return through this hook so they hand back the concrete type instead ofAbstractPanelElement, keeping fluent chains typed end-to-end.- Specified by:
selfin classAbstractPanelElement<ProgressBar>
-
spec
Returns anElementSpecfor a default-styled progress bar (left-to-right, default colors, no label).valueis aDoubleSupplierreturning a normalized0.0–1.0progress — the canonical numeric-supplier shape — so aSlider'sdoublevalue feeds this with no cast:ProgressBar.spec(w, h, () -> s.slider). -
getWidth
-
getHeight
-
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.
-
size
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
Returns the fill direction. -
getFillColor
public int getFillColor()Returns the ARGB fill color. -
getBgColor
public int getBgColor()Returns the ARGB background color.
-