# Lesson 9 : The VLToolBars This is the 9th part of the VLDocking Framework for Java Swing applications. This lessons covers the VLToolBars components, an enhanced version of the classic JToolBar Swing component. ## What are the VLToolbars ? ### An enhanced JToolBar The VLToolBar component provides an enhanced user experience for toolbars. ![](toolbar1.jpg) _A set of VLToolBars_ ![](toolbar2.jpg) _Same set arranged vetically on two columns_ Many important usability concerns have been addressed : * A look and feel with an enhanced ("soft") rollover effect * multiple toolbars can be positionned around a central user component, horizontally, vertically, and on multiple rows and columns * Drag and drop support adapted to this new layout, with immediate feedback ### Drag and Drop support A "gripper" at the base of the toolbar can be used as an anchor to drag and drop the toolbar around its container. ### Load and save configuration As with VLDocking, VLToolBars use an XML format to save and reload their positionning. ## Using VLToolBars ### Installing a toolbar Here is an image introducing the components in use with VLToolBars : ![](toolbars_overview.gif) _An overview of the components_ Installing a set of toolbars is easy : * create a `ToolBarContainer` (a specialized JPanel with a BorderLayout) * add you component at the center : container.add(myComp, BorderLayout.CENTER) * get a reference to a ToolBarPanel from the `ToolBarContainer` : container.getToolBarPanelAt(BorderLayout.NORTH) * add one or more VLToolBars to it, using the ToolBarConstraints to specify their relative order. * enjoy the results ! Details of these operations follow. #### Create a `ToolBarContainer` The `ToolBarContainer` is a specialized JPanel with a BorderLayout, which can contain up to four sets of toolbars (on every side). The easiest way to create that container is to use the static method ```java public static ToolBarContainer createDefaultContainer(boolean topToolbar, boolean leftToolBar, boolean bottomToolBar, boolean rightToolBar) ``` The four booleans are used to specify the borders used to contain a set of toolbars (you way want to have a toolbar on top and bottom, but not on left and right border). The central part of the container is dedicated to the user component. #### Accessing borders and specifying layout constraints Once the user component and usable borders are specified, you will need to access the border components, which are `ToolBarPanel`s. A ToolBarPanel is a specialized JPanel with a ToolBarPanelLayout. It can accept components (VLToolBars) and lay them out horizontally or vertically, at different positions. These positions are specified as `ToolBarConstraints`. Here is an example of accessing some `ToolBarPanels`. ```java TooBarContainer container = ToolBarContainer.createDefaultContainer(true, true, true, true); // top border ToolBarPanel topPanel = container.getToolBarPanelAt(BorderLayout.NORTH); VLToolBar toolbar1 = ... VLToolBar toolbar2 = ... topPanel.add(toolbar1, new ToolBarConstraints(0, 0)); // first row, first column topPanel.add(toolbar2, new ToolBarConstraints(0, 1)); // first row, second column // left border ToolBarPanel leftPanel = container.getToolBarPanelAt(BorderLayout.WEST); VLToolBar toolbar3 = ... VLToolBar toolbar3 = ... leftPanel.add(toolbar3, new ToolBarConstraints(0, 0)); // first *column*, first row leftPanel.add(toolbar4, new ToolBarConstraints(0, 1)); // first column,*second row* ``` ![](toolbar3.jpg) _The result of the sample code above_ If you look at the sample code above, you will see that we have used the same constraints to specify different layouts. This is because ToolbarConstraints are expressed in terms of *major order* and *minor order*, which are considered differently whether the toolbar is laid out horizontally or vertically. So here is an expression of the orders depending on the toolbar orientation : ![](major_order_h.jpg) _The major order in an *horizontal* ToolBarPanel is the *row*._ ![](major_order_v.jpg) _The major order in a *vertical* ToolBarPanel is the *column*._ #### Major/minor order collapsing Please note that if a ToolbarConstraints order (major or minor) is too high for the current contained components, it will be reduced to the next available order. For example, if you add a toolbar at (0,7) and there are only 2 toolbars (0,0) and (0,1) at this minor order, the constraints will be reduced to (0,2). #### Adding buttons to a toolbar This is really simple, you can add() a component, or addSeparator() to add a separator between two buttons. If the component added is an instance of JButton, it's UI will be adapted to the one in use for the toolbar. #### Changing the UI for the toolbar There is currently no UI delegate for the VLToolBar, but it is easy to change its look and feel by subclassing it or updating its set of ui related properties :
VLToolBar property | Description | Default Value |
---|---|---|
isRolloverBorderPainted | paints a rollover border when the mouse is over a JButton | true |
isRooloverContentAreaFilled | updates the setContentAreaFilled of a JButton during rollover | false |
isUseCustomUI | if true, replace the JButton ui delegate by a VLButtonUI (provides a soft rounded border) | true |
draggedBorder | the border used by the toolbar when it is dragged | a ToolBarButtonBorder (soft rounded border) |
import org.jdesktop.swingx.painter.gradient.BasicGradientPainter; import com.vlsolutions.swing.toolbars.BackgroundPainter; // declare public class CustomBackgroundPainter extends BasicGradientPainter implements BackgroundPainter { public CustomBackgroundPainter() { super(BasicGradientPainter.GRAY); } /** BackgrounPainer interface impl */ public void paintBackground(JComponent component, Graphics g) { super.paintBackground((Graphics2D) g, component); } }Then you just have to transmit it to a ToolBarPanel : ```java ToolBarPanel topPanel = container.getToolBarPanelAt(BorderLayout.NORTH); topPanel.setOpaque(false); topPanel.setPainter( // set a custom background painter new CustomBackgroundPainter()); ``` Easy, isn't it ? ---- Next : [Lesson 10 - Compound dockables](tutorial10)