Contents

Swinging Duke
Feedback Button
Left ArrowRight Arrow

 

Swing's Dockable, Floatable Toolbar Class

 

The JToolbar class implements dockable, floatable toolbars in Swing. JToolbar, a subclass of JComponent, is designed using the modified MVC architecture that is standard for Swing components.

JToolbar was introduced in a rudimentary, mostly undocumented form in Version 0.3 of Swing. In Version 0.4, a number of new features and capabilities have been added to the JToolbar class. Most significantly, toolbars now use Swing borders and support floating and docking to all sides of a border layout.

Toolbars also support Action objects as well as ActionListener objects. Documentation covering this feature is currently under development and will be provided with the beta release of Swing. Meanwhile, consult the applicable APIs for the most current documentation.


Implementing JToolbar Components

In a nutshell, this is how you implement floatable, dockable toolbars work in Swing:

  1. Add your toolbar to a container with a BorderLayout.
  2. In the background of your toolbar (but not in a container -- borders and separators are best for this!), do a mouse press and drag. You will then see a window which follows the mouse.
  3. When you release the mouse, the toolbar becomes "floating" -- that is, it appears in a separate window.
  4. Press and drag the mouse again. The border turns red when you are over a potential docking site. The orientation of the window dictates the orientation that the toolbar will assume when it docks.

And that's it! That's all there is to implementing a floatable, dockable toolbar in Swing!


How the JToolbar Class Works

The JToolbar class, like the JMenu class, is a "helper" class that helps developers do what they would otherwise have to do by hand: It creates a button or a menu item, adds the action that the control is designed to handle as an action listener, lets the control listen for property changes on the action, and takes the appropriate action when a property changes.

To perform these tasks, toolbars share the same model which is used for menus -- namely, a "command collection model." In Swing, the command collection model is designed to hold a collection of objects instantiated from subclasses of the Action class. These objects can be logically grouped by the user and can be displayed by an application in appropriate places -- such as in menus and toolbars. In this way, the command collection model supports customization by developers in a graphical manner.

The model portion of a JToolbar object stores all information about the "tools" that are currently displayed on the toolbar. A tool, roughly speaking, is an instance of the Action class. Multiple toolbars can share the same model, and changes to the model are reflected by all toolbars that use that model.


Adding Components and Actions to Toolbars

It is possible to add components that are not commands to a toolbar. In such a case, the components are added to the toolbar as children, but do not become part of the toolbar's model (and are thus not propagated to other toolbars or menus sharing the same model).

It is also possible to add actions directly to JMenus and JToolBars. That's because menus and toolbars can handle the addition of appropriately decorated components that invoke actions whenever an ActionEvent occurs. Components that are created in this way have the ability to listen to actions for beans property changes, and to update themselves appropriately.

Note that it is possible to add the same action to both a JMenu object and a JToolBar object. Subsequently, changes to the action's bound properties are propagated appropriately.


The ToolbarUI Class

The JToolbar class works in conjunction with a related class named ToolbarUI . A ToolbarUI object is a factory that knows how to build appropriate physical representations of commands. In the future, this operation should become completely configurable by the developer, so that any desired physical manifestation of a command can be created.


The JToolbar API

To learn more about the JToolbar class and how it works, see the package, class, interface, and method descriptions in the JToolbar API.

Arrows


Version 0.5. Last modified10/2/97.
Copyright © 1995-97 Sun Microsystems, Inc. All Rights Reserved.

Sun's Home Page