Scroll Panes

Overview

Sometimes there is more to show than fits on the screen: a list of a hundred songs, a long chat, or a map that is much bigger than the window. A scroll pane shows one part of a large area at a time, and scroll bars let the user move around to reach the rest. In this tutorial, you will find out that every form page already scrolls, put a long list into a scroll pane of its own on a page, and add and remove items while the program runs. You will also explore what scrolling means for people who use a keyboard or a screen reader instead of a mouse.

Goals

You have the following goals for this tutorial:

By the end of this lesson, you can

  • Explain the difference between a scroll pane's view and its scroll region
  • Explain how a form page scrolls, and how it follows the focus
  • Show a long list of controls in a scroll pane on a page
  • Add, insert and remove items in a scroll pane, and keep them reachable from the keyboard
  • Check AI-generated code against the library documentation and the program's requirements

Warm up

Think about the apps you scrolled through today. How did you scroll in each one: a mouse wheel, a finger, a scroll bar, or the arrow keys? Now imagine you could not use a mouse or a touch screen at all. How would you get to the bottom of a long page?

Vocabulary

You will be learning about the following vocabulary words:

Vocabulary
TermDefinition
Scroll paneA control that shows part of a larger area and lets the user move around in it.
ViewThe part of the scroll pane that is shown on the screen at one time.
Scroll regionThe whole area that a scroll pane holds, including the parts not shown right now.
Scroll barA bar along the edge of a scroll pane that shows where the view is and can be dragged to move it.
IndexA number that gives an item's place in a list. The first item has index 0.

New Code to Learn

You will be using the following new pieces of code:

New Code to Learn
Quorum CodeCode ExampleExplanation
FormScrollPaneFormScrollPane songsA scroll pane for a form page. The form puts the controls inside it in the Tab order.
page:Add(item)page:Add(songs)Puts a control that has no Add action of its own on a form page.
SetPixelHeight(number height)songs:SetPixelHeight(250)Sets the height of the view in pixels.
Add(Item2D item)songs:Add(song)Adds an item to the end of the scroll region.
Add(integer index, Item2D item)songs:Add(0, song)Adds an item at the given index. Items after it move down one place.
Remove(integer index)Item2D removed = songs:Remove(0)Removes the item at the given index and returns it.
GetScrollRegionItems()Array<Item2D> items = songs:GetScrollRegionItems()Returns the items in the scroll region in order.
CalculateDefaultFocusOrdering()page:CalculateDefaultFocusOrdering()Works out the page's Tab order again after its controls change.
Focus()song:Focus()Moves the keyboard focus to this control.
SetScrollPercentageY(number percentage)songs:SetScrollPercentageY(0)Moves the view up or down. 0 is the top.

CSTA Standards

This tutorial covers the following 2026 CSTA standards:

For teachers · 2026 CSTA K–12 Computer Science Standards

MS-PRO-PD-13

Middle School · Programming

Use reference documentation in program development.

Also in 2 other lessons

MS-PRO-RD-18

Middle School · Programming

Analyze AI-generated code for accuracy and usability in a programming project.

Only this lesson so far

HS-PRO-RD-18

High School · Programming

Evaluate AI-generated code for accuracy, reliability, and alignment with program requirements.

Only this lesson so far

HS-PRO-TR-19

High School · Programming

Evaluate a computing technology's alignment with design specifications and responsible design values, including its correctness, effectiveness, and user experience.

Also in 7 other lessons

MS-PRO-TR-19

Middle School · Programming

Use systematic strategies to test, refine, and document changes to a computing technology to meet the intended purpose.

Also in 6 other lessons

S1-SWD-UX-04

Specialty I · Software Development

Refine a user interface design using accessibility and responsive design tools.

Also in 13 other lessons

Every standard, and the lessons that cover it

Explore

The view and the scroll region

A scroll pane has two sizes:

  • The view is the box shown on the screen.
  • The scroll region is the whole area that holds the items you add. It can be much bigger than the view. Anything in the region that is outside the view is hidden until the user scrolls to it.

When the region is bigger than the view, the scroll pane shows a scroll bar. The user can turn the mouse wheel over the pane, drag the scroll bar, click the bar's track, or drag a finger on a touch screen to move the view. A scroll pane has no scroll keys yet. With the keyboard, the view moves by following the focus: press Tab to a control outside the view, and the pane scrolls to show it. You will try this below.

Every page already scrolls

You have been using scroll panes since the Forms tutorial without knowing it: in Quorum, a form Page is a kind of ScrollPane. When a page has more rows than fit in the window, it shows a scroll bar.

Replace Main.quorum with this program. It puts 30 song buttons on one page.

use Libraries.Interface.Forms.Form
use Libraries.Interface.Forms.Page
use Libraries.Interface.Controls.Button

Form form
Page page = form:GetMainPage()
page:SetTitle("All Songs")

OutputNameBehavior behavior
integer counter = 1
repeat while counter <= 30
    Button song = page:AddButton("Song " + counter)
    song:SetDescription("Plays song number " + counter + ".")
    song:SetBehavior(behavior)
    counter = counter + 1
end

form:Display()

Each button runs the same behavior, which writes the name of the button that was pressed. Put it in its own file, OutputNameBehavior.quorum:

use Libraries.Interface.Behaviors.Behavior
use Libraries.Interface.Events.BehaviorEvent
use Libraries.Interface.Item

class OutputNameBehavior is Behavior
    action Run(BehaviorEvent event)
        Item item = event:GetItem()
        output "Now playing: " + item:GetName()
    end
end

Run the program. Only the first songs fit in the window, and a scroll bar appears on the right. Now put the mouse aside and press Tab again and again. Each time the focus moves to a song below the window, the page scrolls just far enough to show it. This is how a scroll pane works with the keyboard: it has no keys of its own for scrolling, but it follows the focus. A screen reader reads each song's name and description as it gets the focus.

A scroll pane of its own

A long page is fine when the list is the only thing on it. Often, though, you want a list of a set size among other controls, such as a playlist with buttons below it. For that, put a scroll pane on the page.

On a form, use a FormScrollPane. It is a scroll pane that the form knows how to look inside, so the form puts the controls in it into the page's Tab order, after the controls above it and before the ones below it. (A plain ScrollPane works in a game, but a form doesn't look inside it, so Tab would skip its controls.)

use Libraries.Interface.Forms.Form
use Libraries.Interface.Forms.Page
use Libraries.Interface.Forms.FormScrollPane
use Libraries.Interface.Controls.Button
use Libraries.Interface.Controls.Control

Form form
Page page = form:GetMainPage()
page:SetTitle("Playlist")

FormScrollPane songs
songs:SetName("Song list")
songs:SetPixelHeight(250)

OutputNameBehavior behavior
integer counter = 1
repeat while counter <= 20
    Button song
    song:SetName("Song " + counter)
    song:SetDescription("Plays song number " + counter + ".")
    song:SetHorizontalLayoutMode(song:parent:Control:FILL)
    song:SetBehavior(behavior)
    songs:Add(song)
    counter = counter + 1
end
page:Add(songs)

AddSongBehavior addBehavior
addBehavior:SetSongs(songs, behavior)
Button addButton = page:AddButton("Add a song at the top")
addButton:SetBehavior(addBehavior)

RemoveSongBehavior removeBehavior
removeBehavior:SetSongs(songs)
Button removeButton = page:AddButton("Remove the top song")
removeButton:SetBehavior(removeBehavior)

form:Display()

Some things to notice:

  • page:Add(songs) puts the scroll pane on the page. A page has actions like AddButton for many controls, but not for a scroll pane, so you make it yourself and add it. The page gives it its own row.
  • SetPixelHeight(250) sets the size of the view. A page row is normally one line of text tall. The view is 250 pixels tall and as wide as the page's other rows. The scroll region is as wide as the view and grows taller to fit what you add.
  • The songs are made, not added with AddButton. AddButton is a page action; inside the scroll pane you make each Button and add it with songs:Add.
  • FILL makes each song a whole row. A scroll pane places its items one after another from left to right, starting a new line when one is full. A button is normally only as wide as its text, so several songs would share a line. FILL is a layout mode that makes the button fill the rest of the line. FILL belongs to the Control class, so a Button variable reaches it with parent:Control:. The Layouts tutorial explains layout modes.

Run the program and press Tab. The focus moves from the title into the songs, the list scrolls to follow it, and after Song 20 it moves on to the two buttons below the list.

Blocks

Adding, inserting and removing items

Each item in a scroll pane has an index, a number for its place in the order. The first item you add has index 0, the next has index 1, and so on. In a list like this one, the index is also the item's place on the screen, from the top.

songs:Add(0, song) adds a song at index 0. The song that was there, and everything after it, moves down one place. The index must be one that already exists, or one more than the last index.

There are two ways to remove an item:

  • songs:Remove(song) removes that item. It returns true if the item was in the scroll pane, and false if it was not.
  • songs:Remove(0) removes the item at index 0 and returns it.

A form works out its Tab order once, when it is displayed. After you add or remove an item, ask the page to work it out again with CalculateDefaultFocusOrdering. Otherwise Tab skips the new song, or tries to move to a song that is gone. This one line is all you need here; the Focus tutorial covers the focus order in full.

The two behaviors need the page, so they come from FormBehavior, which has GetPage. Put this class in AddSongBehavior.quorum:

use Libraries.Interface.Forms.FormBehavior
use Libraries.Interface.Forms.FormScrollPane
use Libraries.Interface.Forms.Page
use Libraries.Interface.Events.BehaviorEvent
use Libraries.Interface.Behaviors.Behavior
use Libraries.Interface.Controls.Button
use Libraries.Interface.Controls.Control

class AddSongBehavior is FormBehavior
    FormScrollPane songs = undefined
    Behavior playBehavior = undefined
    integer count = 0

    action SetSongs(FormScrollPane pane, Behavior play)
        songs = pane
        playBehavior = play
    end

    action Run(BehaviorEvent event)
        if songs = undefined
            return now
        end

        count = count + 1
        Button song
        song:SetName("New song " + count)
        song:SetDescription("A song added while the program was running.")
        song:SetHorizontalLayoutMode(song:parent:Control:FILL)
        song:SetBehavior(playBehavior)
        songs:Add(0, song)

        Page page = GetPage()
        page:CalculateDefaultFocusOrdering()
        song:Focus()
    end
end

song:Focus() moves the focus to the new song. The list scrolls to the top to show it, and a screen reader reads its name. Put this class in RemoveSongBehavior.quorum:

use Libraries.Interface.Forms.FormBehavior
use Libraries.Interface.Forms.FormScrollPane
use Libraries.Interface.Forms.Page
use Libraries.Interface.Events.BehaviorEvent
use Libraries.Interface.Item2D
use Libraries.Containers.Array

class RemoveSongBehavior is FormBehavior
    FormScrollPane songs = undefined

    action SetSongs(FormScrollPane pane)
        songs = pane
    end

    action Run(BehaviorEvent event)
        if songs = undefined
            return now
        end

        Array<Item2D> items = songs:GetScrollRegionItems()
        if items:IsEmpty()
            output "The playlist is empty."
            return now
        end

        Item2D removed = songs:Remove(0)
        output "Removed " + removed:GetName()

        Page page = GetPage()
        page:CalculateDefaultFocusOrdering()
    end
end

Scrolling from code

You can also move the view yourself. SetScrollPercentageY moves it up or down, where 0 is the top and 1 is the bottom:

songs:SetScrollPercentageY(0)

You rarely need this for a list of controls, because the pane already follows the focus. It helps with content that can't take the focus, such as a picture. Keep in mind that a keyboard or screen reader user can only scroll by moving the focus, so anything a person must reach should be able to take the focus.

Regions of other shapes

By default, the scroll region is as wide as the view and grows taller to fit its items, which is right for a list. SetScrollRegionResizing chooses another rule:

  • FIT_CONTENTS_VERTICALLY, the default: as wide as the view, as tall as the items;
  • FIT_CONTENTS_HORIZONTALLY: as tall as the view, as wide as the items;
  • MANUAL_REGION_SIZING: the size you give to SetRegionSize, or the size FitRegionToContents works out from the items in it.

A large map that scrolls both ways would use MANUAL_REGION_SIZING and place each item at a position of its own. Placing items at positions is what a ManualLayout does, which you will learn in the next tutorial, Layouts.

Checking code an AI wrote

The Playlist program on this page, with its three behaviors, was written by an AI system called Claude when this tutorial was drafted. So was the rest of the code in this tutorial. AI-generated code can look right and still be wrong. It can call an action that doesn't exist, use a real action the wrong way, or skip something the program was supposed to do. Before you trust code like this, check it, the same way you would check a classmate's work. Textbooks could have issues as well, but this is more rare.

The Playlist program has these requirements, the things it is supposed to do:

  1. It shows 20 songs in a list 250 pixels tall, with two buttons below it.
  2. Every song has a name and a description, and can be reached with Tab.
  3. "Add a song at the top" puts a new song first in the list and moves the focus to it.
  4. "Remove the top song" removes the first song. When the list is empty, it says so and nothing breaks.
  5. After any change, Tab reaches every song in the list, and none that are gone.

Check the code in three ways:

  • Accuracy: does every action it calls exist, and does it do what the code expects? Look each one up in the library documentation, starting with the ScrollPane and FormScrollPane pages. For example, find Remove(integer index) and read what it returns. If a class's page says little about an action, check the class it comes from.
  • Reliability: does it work every time, not just the first time? Try the unusual cases: remove songs until the list is empty, then press Remove again; add several songs in a row; add a song after removing them all.
  • Fit to the requirements: go through the list above one item at a time, and mark each one met, not met, or not sure. Also notice anything the code does that no requirement asked for.

Engage

In this activity, you check the AI-generated Playlist program on your own, then turn it into a scrolling contact list and test it. The goal is to decide, with evidence, whether code you didn't write can be trusted, and to test, fix and record changes to a scroll pane program. Work through these steps:

  1. Check each action. Make a table with one row for every action the Playlist program calls, such as SetPixelHeight, Add, Remove, GetScrollRegionItems, CalculateDefaultFocusOrdering and Focus. For each, write the class whose documentation page describes it, what the documentation says it does, and whether the program uses it correctly. Mark any action you could not find.
  2. Test each requirement. Run the program and test the five requirements from "Checking code an AI wrote", first with the mouse and then with only the keyboard. Include the unusual cases. Mark each requirement met, not met or not sure, and write down what you did that shows it.
  3. Change and fix. Change the program into a contact list with at least 15 made-up contacts, each with a name and a description. Fix any problem you found in steps 1 and 2. Then use your keyboard results to improve one thing. For example, would the buttons be easier to reach above a long list?
  4. Record your changes. For every change, write what you changed, why, and how you tested it afterwards. Then run all five tests again.

Finish with a short verdict: would you trust the AI-generated program as it was, with changes, or not at all? Give your two strongest reasons.

Wrap up

Scrolling is so common that people rarely think about it. What did you learn about how scrolling works for people who do not use a mouse? Which of your checks found something that running the program once would have missed? Would you check code that a person wrote any differently from code that an AI wrote?