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.

1  
// currently doesn't use any gaps or margins
2  
sclass VScrollingWrapLayout > AbstractLayoutManager {
3  
  settable bool centeredHorizontally;
4  
5  
  public void layoutContainer(Container parent) {
6  
    L<Component> components = getComponents(parent);
7  
    
8  
    class X {
9  
      int maxWidth = parent.getWidth();
10  
      int x, y;
11  
      int lineHeight;
12  
      int componentsInLine, i;
13  
      
14  
      void finishLine {
15  
        if (centeredHorizontally) {
16  
          int x1 = (maxWidth-x)/2;
17  
          for (int j = i-componentsInLine; j < i; j++) {
18  
            var c = components.get(j);
19  
            Rectangle r = c.getBounds();
20  
            c.setBounds(r.x+x1, r.y, r.width, r.height);
21  
          }
22  
        }
23  
      }
24  
25  
      void run {
26  
        for (i = 0; i < l(components); i++) {
27  
          var c = components.get(i);
28  
          Dimension size = c.getPreferredSize();
29  
          bool needNewLine = componentsInLine != 0 && x+size.width > maxWidth;
30  
          if (needNewLine) {
31  
            finishLine();
32  
            y += lineHeight;
33  
            x = componentsInLine = lineHeight = 0;
34  
          }
35  
            
36  
          c.setBounds(x, y, size.width, size.height);
37  
          x += size.width;
38  
          lineHeight = max(size.height, lineHeight);
39  
          ++componentsInLine;
40  
        }
41  
        finishLine();
42  
  
43  
        preferredSize(maxWidth, y+lineHeight);
44  
      }
45  
    }
46  
    
47  
    new X().run();
48  
  }
49  
}

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: 82 / 143
Version history: 8 change(s)
Referenced in: [show references]