import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.LayoutManager;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class WrapLayout implements LayoutManager
{
protected int _hgap;
protected int _vgap;
public WrapLayout()
{
this(8, 8);
}
public WrapLayout(final int hgap, final int vgap)
{
_hgap = hgap;
_vgap = vgap;
}
public void addLayoutComponent(final String name, final Component c)
{
}
public void layoutContainer(final Container parent)
{
final int width = parent.getWidth();
int x = 0;
int y = 0;
int current_height = 0;
for (final Component c : parent.getComponents())
{
final Dimension d = c.getPreferredSize();
if (_hgap + x + d.width + _hgap > width && x > 0)
{
y += current_height + _vgap;
x = 0;
current_height = 0;
}
if (d.height > current_height)
{
current_height = d.height;
}
c.setBounds(_hgap + x, _vgap + y, d.width, d.height);
x += d.width + _hgap;
}
}
public Dimension minimumLayoutSize(final Container parent)
{
final Dimension result = new Dimension();
result.width = 0;
result.height = 0;
for (final Component c : parent.getComponents())
{
final Dimension d = c.getPreferredSize();
if (d.width > result.width)
{
result.width = d.width;
}
if (d.height > result.height)
{
result.height = d.height;
}
}
result.width = result.width + _hgap * 2;
result.height = result.height + _vgap * 2;
return result;
}
public Dimension preferredLayoutSize(final Container parent)
{
final Dimension result = new Dimension();
result.width = parent.getWidth();
result.height = _vgap;
int x = 0;
int current_height = 0;
for (final Component c : parent.getComponents())
{
final Dimension d = c.getPreferredSize();
if (_hgap + x + d.width + _hgap > result.width && x > 0)
{
result.height += current_height + _vgap;
x = 0;
current_height = 0;
}
if (d.height > current_height)
{
current_height = d.height;
}
x += d.width + _hgap;
}
if (current_height > 0)
{
result.height += current_height + _vgap;
}
result.width += _hgap;
return result;
}
public void removeLayoutComponent(final Component comp)
{
}
public int getHgap()
{
return _hgap;
}
public int getVgap()
{
return _vgap;
}
public void setHgap(final int hgap)
{
_hgap = hgap;
}
public void setVgap(final int vgap)
{
_vgap = vgap;
}
public static void main(final String[] args)
{
final JFrame frame = new JFrame("WrapDemo");
final JPanel wrapPanel = new JPanel(new WrapLayout());
wrapPanel.add(new JButton("01"));
wrapPanel.add(new JButton("02"));
wrapPanel.add(new JButton("02"));
wrapPanel.add(new JButton("03"));
wrapPanel.add(new JButton("04"));
wrapPanel.add(new JButton("05"));
wrapPanel.add(new JButton("06"));
wrapPanel.add(new JButton("07"));
wrapPanel.add(new JButton("08"));
wrapPanel.add(new JButton("09"));
wrapPanel.add(new JButton("10"));
frame.getContentPane().add(wrapPanel);
frame.pack();
frame.setVisible(true);
frame.addWindowListener(new WindowAdapter()
{
@Override
public void windowClosed(final WindowEvent e)
{
super.windowClosed(e);
System.exit(0);
}
});
}
}