A Dockable Toolbar using the new Event Delegation Model


TEA APIs

See also TEA Editor Installation and Description

Here you find the description of how to build a simple Toolbar component. The code exposed is taken from the Source Codes of the TETRACTYS Enhanced Apis and is extensively used by the TEA Editor.
A Toolbar is a component that contains icons and performs some common tasks. Here we'll see how to add icons that link themselves to the Statusbar showing a descriptive text. We'll see also how to dock the toolbar is different places of the main frame.

Class and Methods

Three are the classes involved in the Toolbar running:

  1. Interface tea.awt.bar.Docker

    This Docker interface defines the fundamental methods for a component that holds a Toolbar. It must be implemented for a correct management of the automatic placement of one or more Toolbars. It also gives a method to deliver messages to a Statusbar.

  2. Class tea.awt.bar.Toolbar

    This container is a Toolbar. It can contain several components of every kind, but its preferred component is the IconButton. It can also be contained by several containers, but its preferred container is a Docker. A Toolbar with IconButtons in a Docker performs some automatic tasks (e.g. Statusbar interaction). Placement of the Toolbar in the Docker takes place through a Popup Menu.
    The Toolbar can also be easily internationalized because implements LanguageSpeaker (internally supported languages are English and Italian).

  3. Class tea.awt.bar.IconButton

    This component is a Button with an image in it. The drawn image may be a gif or a jpeg image. If you use an animated gif image, the icon will be animated. Usually you can use this component in Toolbars, but it's possible to add it in other containers. In a Toolbar the IconButton accesses the Statusbar of the Docker with an explicative message when the mouse enters it.

How to Build a Toolbar

This constructor is taken from the MainToolbar class: this class implements the Toolbar that is used by TEA Editor
	public MainToolbar(ActionListener al) {
	  super();
	  if(toolbarIcon == null) {  // cycle that loads all the necessary images
	    toolbarIcon = new Image[ICON_FILE.length];
	    for(int ct = 0; ct < ICON_FILE.length; ct++) {
	      toolbarIcon[ct] = Toolkit.getDefaultToolkit().getImage(
	        "icons/"+ICON_FILE[ct]+".gif"
	      );
	    }
	  }
	  setIconSize(20);  // set the incon size to 20 pixels (width and height)
	  addIcon(toolbarIcon[NEW_ICON],"New Document","new",al,null);
	  addIcon(toolbarIcon[OPEN_ICON],"Open Document","open",al,null);
	  addIcon(toolbarIcon[SAVE_ICON],"Save Document","save",al,null);
	  ...  // add icons to the toolbar
	}

It's simple to add icons to a Toolbar: use the addIcon() method. It's arguments are in this order: img the image of the icon, c the descriptive text, ac the action command, al the ActionListener, sl the StatusListener.

The icon sends the descriptive text to the StatusListener (the Statusbar) when the pointer of the mouse enters it, while sends the actionCommand to the ActionListener when the user clicks on the icon. For the implementation of the methods of IconButton see the source code included in the documentation of the TEA Apis.

Adding a Toolbar to a Docker

Even adding a Toolbar to a Frame is a simple task. All the work is done by the SingleDocker class, that extends Frame and implements Docker. For example, in the constructor of TextEditor (from which is derived TEA Editor) you find

	MainToolbar toolbar = new MainToolbar(actionManager);  // Builds the Toolbar
	addToolbar(toolbar); // Adds the Toolbar to the Docker

The SingleDocker constructor simply defines the Statusbar and places it.

	public SingleDocker(String title, String copyright) {
	  super(title);
	  statusbar = new Statusbar(copyright);
	  setLayout(new BorderLayout());
	  add(statusbar,"South");
	}

The method addToolbar(Toolbar) adds the Toolbar to this Docker and does nothing if a Toolbar is already present.

	public void addToolbar(Toolbar tb) {
	  if(noToolbar) {
	    toolbar = tb;
	    toolbar.setStatusListener(statusbar);
	    toolbar.setOrientation(Toolbar.HORIZONTAL);
	    if(place == null) place = "North";
	    add(toolbar, place);
	    noToolbar = false;
	  }
	}

Notice the setStatusListener(StatusListener) method that sets the Statusbar for the descriptive text. The add(Component,Object) method is the usual one in Containers: SingleDocker has a BorderLayout and acts consequently. The Docker must orientate the Toolbar in the right direction before adding it.

Moving the Toolbar

With the setPlace(Toolbar,String) method it's possible to move the Toolbar in another docking location. The user chooses the new location through a Popup Menu. Then the Toolbar, that is an ActionListener, receives the action with actionPerformed(ActionEvent)

	public void actionPerformed(ActionEvent ae) {
	  Container parent = getParent();
	  if(parent instanceof Docker)
	    ((Docker)getParent()).setPlace(this,ae.getActionCommand())
	  ;
	}
and uses the setPlace(Toolbar,String) method to order the movement.

These are some tips used in the Development of the TEA Apis. I know they are fragmented and quite superficial, but the complete source code is available and you can write directly to TETRACTYS Freeware for any question or suggestion.


TETRACTYS Freeware Main Page

In the next Issue Java Tutorials will be

AWT: Listenting to Large Number of Actions

GeoCities