Skip to content
CrossFly is 100% open sourceFollow us on GitHub
CrossFlyCrossFly
Get started

This page was machine-translated from Portuguese and may contain errors. The Portuguese version is canonical.

RandomLayout

It arranges nothing automatically: each child needs bounds. It is the default layout when you call add(...) without setting a layout. Despite the name, it is the foundation of overlays and floating cards.

🔴 · Package: dev.crossfly.ui.layout · Extends: Layout

When to use

  • Overlays, floating cards, free positioning.
  • Custom layouts where you control the bounds.

When NOT to use

  • Normal screens → use a layout that arranges (Vertical/Horizontal/Grid).

add(...)

public Layout add(Object... params);   // only collects the components; does not position them

Two ways to position

  1. Manual (pixels — NOT responsive): filho.setBounds(x, y, w, h).
  2. Relative (responsive — recommended): filho.setRelativeBounds(Relative x, Relative y, Relative w, Relative h). RandomLayout’s doLayout() calls Relative.setBounds(c) automatically for every child with relative bounds.

Full example (copy, paste and run)

package com.exemplo;

import dev.crossfly.ui.*;
import dev.crossfly.ui.layout.*;
import dev.crossfly.ui.graphics.*;

/** Runnable main screen. Point the launcher (classname) and run: gradlew :lwjgl3:run */
public class MainWindow extends Window
{
   @Override
   public void onShow() throws Exception
   {
      super.onShow();

      // The Window itself uses RandomLayout by default (no setLayout)
      Component card = new Component();
      card.setLayout(new VerticalLayout(Position.START, Position.START));
      card.add(LAY_AUTO, new Label("Cartão flutuante"));
      card.setRelativeBounds(
         Relative.CENTER,                            // x: centered
         Relative.CENTER,                            // y: centered
         Relative.ofScreen(0.8f, ScreenSize.MIN),   // width: 80% of the smaller side
         Relative.ofFont(10f)                       // height: 10 font lines
      );
      add(card);                                     // RandomLayout resolves the relative bounds
   }
}

The snippets below show only the core (what goes inside onShow()). To run them, place them in the MainWindow skeleton above.

Basic usage (responsive centered card)

Component overlay = new Component();          // no setLayout → RandomLayout
Component card = new Component();
card.setRelativeBounds(
   Relative.CENTER,                            // x: centered
   Relative.CENTER,                            // y: centered
   Relative.ofScreen(0.8f, ScreenSize.MIN),   // width: 80% of the smaller side
   Relative.ofFont(10f)                       // height: 10 font lines
);
overlay.add(card);                             // RandomLayout resolves the relative bounds

Best practices

  • Prefer setRelativeBounds over setBounds — it keeps responsiveness.
  • If you must use fixed setBounds, recompute it in Window.onReposition() (called on resize).

Common mistakes

  • Using fixed setBounds(x,y,w,h) and having the UI break on another screen/density.
  • Forgetting that without bounds (neither fixed nor relative) the child stays at (0,0) with size 0.

See also

Was this page helpful?