import java.awt.*;
import java.awt.event.*;
import java.awt.event.ActionListener;

import javax.swing.*;

/**
 * @author Remi Forax
 *
 */
public class FlowLayoutExample {

  private static void addButton(final JPanel panel,String text,final int align) {
    JToggleButton button=new JToggleButton(text);
    button.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        FlowLayout layout=(FlowLayout)panel.getLayout();
        layout.setAlignment(align);
        
        layout.layoutContainer(panel);
        //ou panel.revalidate();
      }
    });
    panel.add(button);
  }

  public static void main(String[] args) {
    JFrame frame=new JFrame("FlowLayoutExample");
    
    LayoutManager layout=new FlowLayout();
    JPanel panel=new JPanel(layout);
    
    addButton(panel,"Left",FlowLayout.LEFT);
    addButton(panel,"Center",FlowLayout.CENTER);
    addButton(panel,"Right",FlowLayout.RIGHT);
    
    ButtonGroup group=new ButtonGroup();
    for(int i=0,count=panel.getComponentCount();i<count;i++)
      group.add((AbstractButton)panel.getComponent(i));
    
    frame.setContentPane(panel);
    frame.pack();
    frame.show();
  }
}
