« prev next »
Java Docking Home> Developer Guide> How to Use Buttons and Tool Bars

How to Use Buttons and Tool Bars

:: Button Dockables ::

If you need a button that can be moved in its tool bar, that can be dragged to other tool bars, or that can be made floating, you have to create a ButtonDockable around your button:

		// Create the button.
		JButton button = new JButton(action);

		// Create the dockable with the button as component.
		ButtonDockable buttonDockable = new ButtonDockable(id, button);
You can also use other components in the button dockable:
		// Create the button.
		ToolBarButton button = new ToolBarButton(action);

		// Create the dockable with the button as component.
		ButtonDockable buttonDockable = new ButtonDockable(id, button);
The main difference between button dockables and normal dockables is their docking mode: If you want to create a button that can't float, you have to specify its docking modes explicitely:
		// Create the dockable with the button as component and that can't float.
		ButtonDockable buttonDockable = new ButtonDockable(id, button, DockingMode.TOOL_BAR);
The normal docks don't accept the docking modes of a button dockable, so button dockables will not be docked in these docks. Their are special docks that accept the docking modes for button dockables.

Button dockables can also not be maximized or minimized.

:: Using special Line Docks and Grid Docks for Buttons ::

You can create a horizontal or vertical LineDock for buttons like this:

		LineDock toolBarDock1 = new LineDock(LineDock.ORIENTATION_HORIZONTAL, false, DockingMode.HORIZONTAL_TOOLBAR, DockingMode.VERTICAL_TOOLBAR);
		...
		LineDock toolBarDock5 = new LineDock(LineDock.ORIENTATION_VERTICAL, false, DockingMode.HORIZONTAL_TOOLBAR, DockingMode.VERTICAL_TOOLBAR);
The third parameter is the docking mode that is used when the orientation is horizontal, the fourth parameter is the docking mode that is used when the orientation is vertical.

You can create a GridDock for buttons like this:

		GridDock toolGridDock = new GridDock(DockingMode.TOOL_GRID);

:: Using special Border Docks and composite Line Docks for Buttons ::

You can create a horizontal or vertical CompositeLineDock for buttons like this:

		CompositeLineDock compositeToolBarDock1 = new CompositeLineDock(CompositeLineDock.ORIENTATION_HORIZONTAL, false,
				new ToolBarDockFactory(), DockingMode.HORIZONTAL_TOOLBAR, DockingMode.VERTICAL_TOOLBAR);
		CompositeLineDock compositeToolBarDock2 = new CompositeLineDock(CompositeLineDock.ORIENTATION_VERTICAL, false,
				new ToolBarDockFactory(), DockingMode.HORIZONTAL_TOOLBAR, DockingMode.VERTICAL_TOOLBAR);
Add the line docks with the button dockables:
		compositeToolBarDock1.addChildDock(toolBarDock1, new Position(0));
		compositeToolBarDock1.addChildDock(toolBarDock2, new Position(1));
		compositeToolBarDock1.addChildDock(toolBarDock3, new Position(2));
		compositeToolBarDock1.addChildDock(toolBarDock4, new Position(3));
		compositeToolBarDock2.addChildDock(toolBarDock5, new Position(0));
		compositeToolBarDock2.addChildDock(toolBarDock6, new Position(1));
		compositeToolBarDock2.addChildDock(toolBarDock7, new Position(2));
You can create a BorderDock for tool bars like this:
		BorderDock toolBarBorderDock = new BorderDock(new CompositeToolBarDockFactory(), splitDock);
		toolBarBorderDock.setMode(BorderDock.MODE_TOOL_BAR);
If this dock is the root dock, it has to be added to the DockModel:
		dockModel.addRootDock("borderDock", toolBarBorderDock, frame);
Add the composite line docks at the borders:
		toolBarBorderDock.setDock(compositeToolBarDock1, Position.TOP);
		toolBarBorderDock.setDock(compositeToolBarDock2, Position.LEFT);




The source code of the samples can be found in:

JButtonExampleShows JButtons in a tool bar at the borders of a window.
ToolBarButtonExampleShows custom buttons in a tool bar at the borders of a window and in a floating grid.



« prev next »