import java.awt.Color;
import java.util.Random;

import javax.swing.*;

/**
 * @author Remi Forax
 *
 */
public class SplitPaneExample {

  private static JComponent createSplits(int depth) {
    if (depth==0) {
      JLabel label=new JLabel();
      label.setOpaque(true);
      label.setBackground(randomColor());
      return label;
    }
    
    JSplitPane pane=new JSplitPane(randomOrientation(),true,
      createSplits(depth-1),
      createSplits(depth-1));
    pane.setOneTouchExpandable(true);
    pane.setResizeWeight(0.5);
    return pane;
  }
  
  private static Color randomColor() {
    return new Color(
      random.nextInt(256),
      random.nextInt(256),
      random.nextInt(256));
  }
  
  private static int randomOrientation() {
    return (random.nextInt(2)==0)?
      JSplitPane.HORIZONTAL_SPLIT:
      JSplitPane.VERTICAL_SPLIT;
  }
  
  private static final Random random=new Random(0);

  public static void main(String[] args) {
    JFrame frame=new JFrame("SplitPaneExample");
    frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    frame.setContentPane(createSplits(4));
    frame.setSize(400,300);
    frame.show();
  }
}
