Toolbar Beginner

Hello!

I’m trying to figure out how to add a toolbar to my existing project. It’s not clear to me how I’d switch from something like I have below, to something that has a toolbar across the top of the entire dockable area.

   {
   	JFrame editorFrame = new JFrame();
   	editorFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   	editorFrame.setLayout(new GridLayout(1, 1));

   	mainControl = new CControl(editorFrame);
   	ToolComponentFactory factory = new ToolComponentFactory();
   	mainControl.addSingleDockableFactory(factory, factory);

   	CControlPerspective controlPerspectives = mainControl.getPerspectives();
   	CPerspective perspective = controlPerspectives.createEmptyPerspective();

   	CGridPerspective center = perspective.getContentArea().getCenter();
   	center.gridAdd(0, 0, 79, 400, new SingleCDockablePerspective("MyTool"));
   	center.gridAdd(80, 300, 319, 99, new SingleCDockablePerspective("MyOtherTool"));

   	CStackPerspective stack = new CStackPerspective();
   	stack.add(new SingleCDockablePerspective("MyStackedTool"));
   	stack.add(new SingleCDockablePerspective("MyOtherStackedTool"));
   	stack.setSelection(stack.getDockable(0));
   	center.grid().addDockable(80, 0, 319, 300, stack);

   	controlPerspectives.setPerspective("main", perspective);
   	mainControl.load("main");
   	mainControl.setTheme(ThemeMap.KEY_ECLIPSE_THEME);

   	// editorFrame.add(mainControl.getContentArea());
   	editorFrame.setBounds(10, 10, 400, 400);
   	editorFrame.setVisible(true);
   	return editorFrame;
   }```

I was basically hoping to be able to push a toolbar into the given CControl, but it doesn't appear to work like that. I was trying to go from tutorial.toolbar.common.CommonPerspective, but it's unclear how I'd do that.

*** Edit ***

Here's the actual implementation that ended up working for me. The trick is in grabbing the center out of the CToolbarContentPerspective (content.getCenter()), and then adding all of the original dockables we want in there.

```import java.awt.Color;

import javax.swing.JFrame;

import bibliothek.gui.dock.common.CControl;
import bibliothek.gui.dock.common.DefaultSingleCDockable;
import bibliothek.gui.dock.common.SingleCDockable;
import bibliothek.gui.dock.common.SingleCDockableFactory;
import bibliothek.gui.dock.common.action.CButton;
import bibliothek.gui.dock.common.perspective.CControlPerspective;
import bibliothek.gui.dock.common.perspective.CGridPerspective;
import bibliothek.gui.dock.common.perspective.CPerspective;
import bibliothek.gui.dock.common.perspective.CStackPerspective;
import bibliothek.gui.dock.common.perspective.SingleCDockablePerspective;
import bibliothek.gui.dock.common.theme.ThemeMap;
import bibliothek.gui.dock.toolbar.CToolbarContentArea;
import bibliothek.gui.dock.toolbar.CToolbarItem;
import bibliothek.gui.dock.toolbar.perspective.CToolbarContentPerspective;
import bibliothek.util.Filter;
import bibliothek.util.filter.RegexFilter;

public class ToolBar
{
   private static final String TOOLBAR_PERSPECTIVE_ID = "center";
   private CPerspective perspective;
   private CControl control;

   public static void main(String[] args)
   {
   	new ToolBar().showWinder();
   }

   private void showWinder()
   {
   	/* As usual we need a frame and a CControl */
   	JFrame frame = new JFrame();
   	control = new CControl(frame);

   	/*
   	 * We initialize the elements that are always present, namely the
   	 * CToolbarContentArea
   	 */
   	CToolbarContentArea area = new CToolbarContentArea(control, TOOLBAR_PERSPECTIVE_ID);
   	control.addStationContainer(area);
   	frame.add(area);

   	/*
   	 * We can use SingleCDockableFactories to lazily create the buttons.
   	 * Here we are using the unique identifier of the buttons to decide
   	 * which icon the button should have.
   	 */
   	control.addSingleDockableFactory(new RegexFilter("red.*"), new ColorToolbarItemFactory(Color.RED));
   	control.addSingleDockableFactory(new RegexFilter("green.*"), new ColorToolbarItemFactory(Color.GREEN));
   	control.addSingleDockableFactory(new RegexFilter("blue.*"), new ColorToolbarItemFactory(Color.BLUE));

   	/*
   	 * Now we access the perspective API, we start by creating an empty
   	 * perspective
   	 */
   	perspective = control.getPerspectives().createEmptyPerspective();
   	/*
   	 * We access the perspective of the CToolbarContentArea by using the
   	 * same unique identifier as we used when creating the area.
   	 */
   	CToolbarContentPerspective content = new CToolbarContentPerspective(perspective, TOOLBAR_PERSPECTIVE_ID);

   	/*
   	 * The buttons are added by simulating drag and drop operations. Imagine
   	 * the empty application, then dropping the button "red-1", followed by
   	 * "red-2", etc.
   	 */
   	content.getNorthToolbar().group(0).column(0).toolbar(0).add("red-1");
   	content.getNorthToolbar().group(0).column(0).toolbar(0).add("red-2");

   	content.getNorthToolbar().group(0).column(0).toolbar(1).add("green-1");
   	content.getNorthToolbar().group(0).column(0).toolbar(1).add("green-2");
   	content.getNorthToolbar().group(0).column(0).toolbar(1).add("green-3");

   	content.getNorthToolbar().group(0).column(1).toolbar(0).add("blue-1");
   	content.getNorthToolbar().group(0).column(1).toolbar(0).add("blue-2");
   	content.getNorthToolbar().group(0).column(1).toolbar(0).add("blue-3");

   	// WRAPPING OLD IMPL

   	ToolComponentFactory factory = new ToolComponentFactory();
   	control.addSingleDockableFactory(factory, factory);
   	CGridPerspective center = content.getCenter();
   	center.gridAdd(0, 0, 79, 400, new SingleCDockablePerspective("MyTool"));
   	center.gridAdd(80, 300, 319, 99, new SingleCDockablePerspective("MyOtherTool"));

   	CStackPerspective stack = new CStackPerspective();
   	stack.add(new SingleCDockablePerspective("MyStackedTool"));
   	stack.add(new SingleCDockablePerspective("MyOtherStackedTool"));
   	stack.setSelection(stack.getDockable(0));
   	center.grid().addDockable(80, 0, 319, 300, stack);
   	control.setTheme(ThemeMap.KEY_ECLIPSE_THEME);

   	CControlPerspective controlPerspectives = control.getPerspectives();
   	controlPerspectives.setPerspective("main", perspective);
   	control.load("main");

   	// WRAPPING OLD IMPL

   	/* Once our initial layout has been created, we apply it. */
   	control.getPerspectives().setPerspective(perspective, true);

   	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   	frame.setBounds(10, 10, 400, 400);
   	frame.setVisible(true);
   }

   /* This factory creates new CToolbarItems (buttons on a toolbar) */
   private static class ColorToolbarItemFactory
       implements SingleCDockableFactory
   {
   	private final Color color;

   	public ColorToolbarItemFactory(Color color)
   	{
   		this.color = color;
   	}

   	@Override
   	public SingleCDockable createBackup(String id)
   	{
   		CToolbarItem item = new CToolbarItem(id);
   		item.setItem(new CButton("Action", new ColorIcon(color)));
   		return item;
   	}
   }

   private class ToolComponentFactory
       implements SingleCDockableFactory, Filter<String>
   {
   	@Override
   	public boolean includes(String item)
   	{
   		return true;
   	}

   	@Override
   	public SingleCDockable createBackup(String id)
   	{
   		DefaultSingleCDockable dockable = new DefaultSingleCDockable(id, id);
   		dockable.setCloseable(true);
   		return dockable;
   	}
   }
}```

its not clear to me how you could miss the first hit in gooogle:
https://www.google.com/url?q=http://docs.oracle.com/javase/tutorial/uiswing/components/toolbar.html&sa=U&ei=WSeDUsaQL8XEtQawnoGgAQ&ved=0CAsQFjAA&usg=AFQjCNF2KDCW5nDgugR518UFT7RW_F5lmg

bye
TT

Hey Timothy,

I was referencing the DockingFrames toolbar, not the JToolBar. The CToolbarContentArea works a bit different. Thanks for the helpful comment, though.

I would have written the same code amish. :slight_smile:

… perhaps the tutorials need a small upgrade to show your usecase as well.