« prev next »
Java Docking Home> Developer Guide> How to Use Dockables

How to Use Dockables

:: A dockable for a UI component ::

If you want to add docking functionality to a UI component, you have to create a Dockable for it. Give your component as content to the dockable.

		// Create the content component.
		TextPanel textPanel = new TextPanel("I am window 1.");

		// Create the dockable around the content component.
		Dockable dockable = new DefaultDockable("Window1", textPanel, "Window", null, DockingMode.ALL);
The first parameter for the DefaultDockable is the ID of the dockable. The IDs of all the dockables in your application should be different.

:: A dockable with an icon and a description ::

You can given the dockable an icon and a description. The icon will be displayed in the header of the dock, where the dockable is docked. The description will appear as tooltip above the header.

		// Create the dockable around the content component.
		Icon icon = new ImageIcon(getClass().getResource("/com/javadocking/resources/images/text12.gif"));
		DefaultDockable dockable = new DefaultDockable("Window1", textPanel, "Window", icon);
		dockable.setDescription("Window with text");	

:: Specifying the docks where a dockable can be docked ::

You can specify in what kind of docks the dockable may be docked by setting the docking mode. The different docking modes are defined in the class DockingMode.

In the folowing example the dockables can't float.

		// We don't want the dockables to float.
		int dockingModes = DockingMode.ALL - DockingMode.FLOAT;
		
		// Create the dockables around the content components.
		Dockable dockable = new DefaultDockable("Window1", textPanel, "Window", icon, dockingModes);

:: A dockable with actions ::

You can decorate a dockable with actions by using an ActionDockable. First you have to create your basic dockable. Then you create the wrapper action dockable around the dockable, that adds actions to it.

In the folowing example the dockable is first decorated with a close action. Then it is decorated with 2 other actions.

		// Create the dockable around the content component.
		Dockable dockable = new DefaultDockable("Window1", textPanel, "Window", new ImageIcon("resources/images/text12.gif"));

		// Decorate the dockable with a close action.		
		dockable = new StateActionDockable(dockable, new DefaultDockableStateActionFactory(), DockableState.STATES_CLOSED);
		
		// Decorate the dockable with another action.
		MessageAction helloAction = new MessageAction(this, "Hello", new ImageIcon("/com/javadocking/resources/images/hello12.gif"), "Hello world!");
		MessageAction cautionAction = new MessageAction(this, "Caution", new ImageIcon("/com/javadocking/resources/images/caution12.gif"), "Be Careful!");
		Action[][] actions = new Action[1][];
		actions[0] = new Action[2];
		actions[0][0] = helloAction;
		actions[0][1] = cautionAction;
		dockable = new ActionDockable(dockable, actions);

:: Dragging functionality on the content of the dockable ::

A dockable can always be dragged by dragging the header of the dock, in which it is docked. It is also possible to drag the dockable by dragging its content.

If you want that, the content of your dockable should implement the interface DraggableContent. This interface contains one method addDragListener(DragListener). The drag listener should be added as mouse listener and mouse motion listener on every component with which you want to be able to drag the dockable.

A possible implementation is:

		public void addDragListener(DragListener dragListener)
		{
			this.addMouseListener(dragListener);
			this.addMouseMotionListener(dragListener);
			label.addMouseListener(dragListener);
			label.addMouseMotionListener(dragListener);
		}



The source code of the samples can be found in:

SimpleDockableShows a simple dockable.
IconDockableShows a dockable with an icon and tooltip.
NotFloatingDockableShows a dockable that cannot float.
ActionsDockableShows a dockable with actions.



« prev next »