Toolbars

Overview

Below the menu bar in many programs is a row of small buttons for the commands people use most, like Save, Undo or Bold. This row is a toolbar. A toolbar is a shortcut: every button on it usually does the same thing as a command in a menu, but it takes one click instead of several. In this tutorial, you will put a toolbar on a page, connect its buttons to behaviors, and share those behaviors with a menu bar so that both do the same thing. You will also learn how a keyboard user moves along a toolbar with the arrow keys.

Goals

You have the following goals for this tutorial:

By the end of this lesson, you can

  • Create a ToolBar, fill it with named, described buttons, and put it on a page
  • Move between toolbar buttons with the arrow keys
  • Share one behavior between a toolbar button and a menu command
  • Decide which commands belong on a toolbar

Warm up

Open a program that has a toolbar, like a word processor, a drawing program or a web browser. List the buttons on its toolbar. Then find each of those commands in the program's menus. Are there any toolbar buttons that are not in a menu? Why might the designers have picked these commands, and not others, for the toolbar?

Vocabulary

You will be learning about the following vocabulary words:

Vocabulary
TermDefinition
ToolbarA row of buttons for the commands people use most. It usually sits just below the menu bar.
CommandAn action a user can ask a program to do. The same command can be on a menu and on a toolbar.
Shared behaviorOne behavior object used by more than one control so that they all do the same thing.
Arrow-key navigationMoving the keyboard focus between the controls in a group with the arrow keys instead of the Tab key.

New Code to Learn

You will be using the following new pieces of code:

New Code to Learn
Quorum CodeCode ExampleExplanation
ToolBarToolBar toolsCreates an empty toolbar. It is as wide as whatever holds it, 30 pixels high, and lines up its buttons from left to right.
Add(Item2D item)tools:Add(button)Adds a button or other control to the end of the toolbar.
SetName(text name)tools:SetName("Animal sounds")Gives the toolbar the name a screen reader reads.
SetBehavior(Behavior behavior)button:SetBehavior(behavior)Sets the behavior a button or menu item runs. Two controls can be given the same behavior.

CSTA Standards

This tutorial covers the following CSTA standards:

For teachers · 2026 CSTA K–12 Computer Science Standards

HS-PRO-TR-20

High School · Programming

Refine a computing technology based on user feedback, testing results, and responsible design values to improve its effectiveness and impact.

Also in 4 other lessons

MS-PRO-TR-20

Middle School · Programming

Refine a computing technology based on user feedback to improve its usability and accessibility.

Also in 7 other lessons

S1-SWD-UX-05

Specialty I · Software Development

Create software that serves intended users and contexts using human-centered design principles.

Also in 3 other lessons

HS-PRO-PD-14

High School · Programming

Apply appropriate attribution of intellectual property when developing a computing technology.

Also in 3 other lessons

MS-PRO-PD-14

Middle School · Programming

Justify the importance of attribution and intellectual property when developing computing technologies.

Also in 1 other lesson

Every standard, and the lessons that cover it

Explore

What a toolbar does

A ToolBar is a control that holds other controls, usually buttons, in a single row. On its own, it does these things for you:

  • it stretches across the full width of whatever holds it, and is 30 pixels high;
  • it lines its buttons up from left to right;
  • it turns on a tooltip for each control you add, so pointing at a button with the mouse shows a small hint, and so does pressing Shift+F1 on the button that has the focus (Esc closes it);
  • it tells a screen reader that it is a toolbar, and lets the user move between its buttons with the arrow keys.

A toolbar should hold only the commands people use most. If every command went on the toolbar, it would be as hard to search as a menu.

Building a toolbar

This tutorial uses OutputBehavior from the Menus tutorial. Add a class with that name:

use Libraries.Game.Graphics.Label
use Libraries.Interface.Behaviors.Behavior
use Libraries.Interface.Events.BehaviorEvent

class OutputBehavior is Behavior
    Label label = undefined
    text message = ""

    action Run(BehaviorEvent event)
        if label not= undefined
            label:SetText(message)
        end
        output message
    end

    action SetLabel(Label newLabel)
        label = newLabel
    end

    action SetMessage(text newMessage)
        message = newMessage
    end
end

A page has no AddToolBar action, so you make the toolbar yourself and give it to the page's plain Add action, as you did with the menu bar. Add it first, so it is the top row of the page.

Now replace Main.quorum. MakeButton builds one button, gives it a name, a description and an OutputBehavior, and returns it:

use Libraries.Interface.Forms.Form
use Libraries.Interface.Forms.Page
use Libraries.Game.Graphics.Label
use Libraries.Interface.Controls.Button
use Libraries.Interface.Controls.ToolBar

class Main
    Label message = undefined

    action Main
        Form form
        Page page = form:GetMainPage()

        ToolBar tools
        tools:SetName("Animal sounds")
        tools:SetDescription("Use the left and right arrow keys to move between the buttons")
        page:Add(tools)

        message = page:AddLabel("Message", "Press a button on the toolbar.")

        tools:Add(MakeButton("Dog", "Woof!"))
        tools:Add(MakeButton("Cat", "Meow!"))
        tools:Add(MakeButton("Cow", "Moo!"))
        tools:Add(MakeButton("Duck", "Quack!"))

        form:Display()
    end

    action MakeButton(text name, text sound) returns Button
        OutputBehavior behavior
        behavior:SetLabel(message)
        behavior:SetMessage(sound)

        Button button
        button:SetName(name)
        button:SetDescription("Shows what the " + name + " says")
        button:SetBehavior(behavior)
        return button
    end
end

SetName replaces the toolbar's default name, "Toolbar", with one that tells a screen reader user what the buttons are for.

Using a toolbar with the keyboard

A toolbar is one stop in the page's Tab order. You press Tab to reach the toolbar, then use the arrow keys to move between its buttons. Because the toolbar is first on this page, it has the focus when the app starts. When the toolbar or any of its buttons has the focus, these keys work:

A toolbar's keys
KeyWhat it does
Right arrow or Down arrowMoves the focus to the next button. After the last button it goes back to the first.
Left arrow or Up arrowMoves the focus to the previous button. Before the first button it goes to the last.
Space or EnterPresses the button that has the focus.
Tab or Shift+TabLeaves the toolbar for the next or previous control on the page.

When the toolbar itself has the focus, pressing Right or Down moves the focus to its first button, and pressing Left or Up moves it to its last button.

Tab and the toolbar. The form sets the Tab order for the controls on the page: here, the toolbar and the message. The buttons are inside the toolbar, so Tab doesn't stop on each one. The arrow keys move between the buttons, and Tab or Shift+Tab on any button moves on to the next or previous control on the page. This follows the toolbar pattern in the W3C's WAI-ARIA Authoring Practices, a guide to accessible controls.

Sharing behaviors with a menu bar

Most toolbar buttons are copies of menu commands. When a button and a menu item should do exactly the same thing, give them the same behavior object. Then there is only one place to change if the command changes.

The finished program below has a menu bar, then a toolbar, then the message. The AddCommand action makes one OutputBehavior, then makes a menu item and a toolbar button that both use it.

use Libraries.Interface.Forms.Form
use Libraries.Interface.Forms.Page
use Libraries.Game.Graphics.Label
use Libraries.Interface.Controls.Button
use Libraries.Interface.Controls.MenuBar
use Libraries.Interface.Controls.MenuItem
use Libraries.Interface.Controls.ToolBar

class Main
    MenuBar bar
    ToolBar tools
    Label message = undefined

    action Main
        Form form
        Page page = form:GetMainPage()

        bar:SetName("Main menu")
        page:Add(bar)

        tools:SetName("Animal sounds")
        tools:SetDescription("Use the left and right arrow keys to move between the buttons")
        page:Add(tools)

        message = page:AddLabel("Message", "Choose an animal from the menu or the toolbar.")

        MenuItem animals
        animals:SetName("Animals")
        animals:SetMnemonic("a")
        bar:Add(animals)

        AddCommand(animals, "Dog", "Woof!")
        AddCommand(animals, "Cat", "Meow!")
        AddCommand(animals, "Cow", "Moo!")
        AddCommand(animals, "Duck", "Quack!")

        form:Display()
    end

    action AddCommand(MenuItem menu, text name, text sound)
        OutputBehavior behavior
        behavior:SetLabel(message)
        behavior:SetMessage(sound)

        MenuItem item
        item:SetName(name)
        item:SetBehavior(behavior)
        menu:Add(item)

        Button button
        button:SetName(name)
        button:SetDescription("Shows what the " + name + " says")
        button:SetBehavior(behavior)
        tools:Add(button)
    end
end

Run it. Choosing Animals, then Dog, and pressing the Dog button on the toolbar now do the same thing, because they run the same OutputBehavior. The form puts the menu bar, the toolbar and the message in the Tab order, from top to bottom. On a toolbar button, Tab goes on to the message and Shift+Tab goes back to the menu bar.

Blocks

Icons on toolbar buttons

Many toolbars show small pictures instead of words. If you give a button an icon, still give it a name with SetName. A screen reader cannot describe a picture; it reads the name. A good rule is that every toolbar button should still make sense from its name alone.

If you use icons that someone else made, they are that person's intellectual property. Check the icon's license, which says whether you may use it and how, and give the credit it asks for, such as the artist's name and where the icon came from, in your program's About command or its documentation.

Engage

In this activity, you and a partner add a toolbar to the menu bar you designed in the Menus tutorial, and test which commands deserve a place on it. The goal is to decide which commands people use most, and to share one behavior between a menu item and a toolbar button. Start by picking the three or four commands you think people will use most, and explain your choice to your partner. Then add a toolbar with those commands, sharing each behavior with its menu item.

When it works, swap programs with your partner, and do a short task in theirs, such as drawing two shapes and saving. Your partner counts how often you use the toolbar and how often the menu. Afterwards, tell your partner which command you reached for and didn't find on the toolbar. Based on their counts and your partner's feedback, move one command onto or off the toolbar.

Wrap up

A toolbar repeats commands that are already in a menu. Why is it worth having the same command in two places? What could go wrong if the toolbar button and the menu item used two different behaviors?