what layoutmanager should use achieve transposed version of flowlayout?
essentially, want vertical list occupies multiple columns if can't fit of it's components within 1 column.
+------------------------+ | item 1 | | item 2 | | item 3 | | item 4 | | item 5 | | item 6 | | item 7 | | item 8 | +------------------------+
or
+------------------------+ | item 1 item 7 | | item 2 item 8 | | item 3 | | item 4 | | item 5 | | item 6 | +------------------------+
this wrapping logic needs happen dynamically, ie container resized.
i'm working on solution my own question (very similar: need flow vertically constrain width horizontally), got quick example sort of working , maybe gets started custom layout manager:
import java.awt.component; import java.awt.container; import java.awt.dimension; import java.awt.layoutmanager2; import java.util.random; import java.util.set; import javax.swing.jframe; import javax.swing.jlabel; import javax.swing.jpanel; import com.google.common.collect.sets; public class verticalflowlayout implements layoutmanager2 { final private set<component> components = sets.newlinkedhashset(); private int hgap = 0; private int vgap = 0; public void sethgap(int hgap) { this.hgap = hgap; } public void setvgap(int vgap) { this.vgap = vgap; } @override public void addlayoutcomponent(component comp, object constraints) { this.components.add(comp); } /* these 3 methods need overridden */ @override public float getlayoutalignmentx(container target) { // todo auto-generated method stub return 0; } @override public float getlayoutalignmenty(container target) { // todo auto-generated method stub return 0; } @override public void invalidatelayout(container target) { // todo auto-generated method stub } @override public void addlayoutcomponent(string name, component comp) { this.components.add(comp); } @override public void layoutcontainer(container parent) { int x = 0; int y = 0; int columnwidth = 0; (component c : this.components) { if (c.isvisible()) { dimension d = c.getpreferredsize(); columnwidth = math.max(columnwidth, d.width); if (y+d.height > parent.getheight()) { x += columnwidth + this.hgap; y = 0; } c.setbounds(x, y, d.width, d.height); y += d.height + this.vgap; } } } /* these 3 methods need overridden */ @override public dimension minimumlayoutsize(container parent) { return new dimension(0,0); } @override public dimension preferredlayoutsize(container parent) { return new dimension(200,200); } @override public dimension maximumlayoutsize(container target) { return new dimension(600,600); } @override public void removelayoutcomponent(component comp) { this.components.remove(comp); } public static void main(string[] args) { jframe frame = new jframe("verticalflowlayouttest"); verticalflowlayout vfl = new verticalflowlayout(); jpanel panel = new jpanel(vfl); vfl.sethgap(20); vfl.setvgap(2); int n = 19; random r = new random(12345); (int = 0; < n; ++i) { jlabel label = new jlabel(labelname(i,r)); panel.add(label); } frame.setcontentpane(panel); frame.pack(); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.setvisible(true); } private static string labelname(int i, random r) { stringbuilder sb = new stringbuilder(); sb.append("label #"); sb.append(i); sb.append(" "); int n = r.nextint(26); (int j = 0; j < n; ++j) sb.append("_"); return sb.tostring(); } }
Comments
Post a Comment