Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

49
LINES

< > BotCompany Repo | #1035946 // VScrollingWrapLayout - like WrapLayout but scrollable vertically [OK]

JavaX fragment (include) [tags: use-pretranspiled]

Transpiled version (9757L) is out of date.

// currently doesn't use any gaps or margins
sclass VScrollingWrapLayout > AbstractLayoutManager {
  settable bool centeredHorizontally;

  public void layoutContainer(Container parent) {
    L<Component> components = getComponents(parent);
    
    class X {
      int maxWidth = parent.getWidth();
      int x, y;
      int lineHeight;
      int componentsInLine, i;
      
      void finishLine {
        if (centeredHorizontally) {
          int x1 = (maxWidth-x)/2;
          for (int j = i-componentsInLine; j < i; j++) {
            var c = components.get(j);
            Rectangle r = c.getBounds();
            c.setBounds(r.x+x1, r.y, r.width, r.height);
          }
        }
      }

      void run {
        for (i = 0; i < l(components); i++) {
          var c = components.get(i);
          Dimension size = c.getPreferredSize();
          bool needNewLine = componentsInLine != 0 && x+size.width > maxWidth;
          if (needNewLine) {
            finishLine();
            y += lineHeight;
            x = componentsInLine = lineHeight = 0;
          }
            
          c.setBounds(x, y, size.width, size.height);
          x += size.width;
          lineHeight = max(size.height, lineHeight);
          ++componentsInLine;
        }
        finishLine();
  
        preferredSize(maxWidth, y+lineHeight);
      }
    }
    
    new X().run();
  }
}

Author comment

Began life as a copy of #1035072

download  show line numbers  debug dex  old transpilations   

Travelled to 2 computer(s): elmgxqgtpvxh, mqqgnosmbjvj

No comments. add comment

Snippet ID: #1035946
Snippet name: VScrollingWrapLayout - like WrapLayout but scrollable vertically [OK]
Eternal ID of this version: #1035946/9
Text MD5: ad6b10525014cd816852925437ae824d
Author: stefan
Category: javax / gui
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-11-26 19:24:10
Source code size: 1421 bytes / 49 lines
Pitched / IR pitched: No / No
Views / Downloads: 148 / 243
Version history: 8 change(s)
Referenced in: [show references]