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.
In a nutshell, this is how you implement floatable, dockable toolbars work in Swing:
And that's it! That's all there is to implementing a floatable, dockable toolbar in Swing!
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.
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 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.
To learn more about the JToolbar class and how it works, see the package, class, interface, and method descriptions in the JToolbar API.
Version 0.5. Last modified10/2/97.
Copyright © 1995-97 Sun
Microsystems, Inc. All Rights Reserved.