How can I create a dockable toolbar?

Scott Stanchfield

Swing provides support for docking toolbars using the BorderLayout layout manager. Simply add a toolbar to a container that has a BorderLayout, set the toolbar to "floatable" and you can drag it out and dock it in any of the four borders. Not as nice as many docking tools, but pretty decent.

Here's a sample:

import java.awt.BorderLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JToolBar;

public class Test {
  public static void main(String[] args) {
    JFrame f = new JFrame("Toolbar Docking Sample");
    JToolBar t1 = new JToolBar();
    t1.add(new JButton("A"));
    t1.add(new JButton("B"));
    t1.add(new JButton("C"));
    t1.add(new JButton("D"));
    t1.add(new JButton("E"));
    t1.add(new JButton("F"));
    f.getContentPane().setLayout(new BorderLayout());
    f.getContentPane().add(t1, BorderLayout.NORTH);