1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
| /*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
import java.awt.*;
public class ModifiedFlowLayout extends FlowLayout
{
public ModifiedFlowLayout()
{
super();
}
public ModifiedFlowLayout(int align)
{
super(align);
}
public ModifiedFlowLayout(int align, int hgap, int vgap)
{
super(align, hgap, vgap);
}
public Dimension minimumLayoutSize(Container target)
{
return computeSize(target, false);
}
public Dimension preferredLayoutSize(Container target)
{
return computeSize(target, true);
}
private Dimension computeSize(Container target, boolean minimum)
{
synchronized (target.getTreeLock())
{
int hgap = getHgap();
int vgap = getVgap();
int w = target.getWidth();
// Let this behave like a regular FlowLayout (single row)
// if the container hasn't been assigned any size yet
if (w == 0)
w = Integer.MAX_VALUE;
Insets insets = target.getInsets();
if (insets == null)
insets = new Insets(0, 0, 0, 0);
int reqdWidth = 0;
int maxwidth = w - (insets.left + insets.right + hgap * 2);
int n = target.getComponentCount();
int x = 0;
int y = insets.top;
int rowHeight = 0;
for (int i = 0; i < n; i++)
{
Component c = target.getComponent(i);
if (c.isVisible())
{
Dimension d =
minimum ? c.getMinimumSize() :
c.getPreferredSize();
if ((x == 0) || ((x + d.width) <= maxwidth))
{
if (x > 0)
{
x += hgap;
}
x += d.width;
rowHeight = Math.max(rowHeight, d.height);
} else
{
x = d.width;
y += vgap + rowHeight;
rowHeight = d.height;
}
reqdWidth = Math.max(reqdWidth, x);
}
}
y += rowHeight;
return new Dimension(reqdWidth+insets.left+insets.right, y);
}
}
} |
Partager