Unit 5 · Lesson 214 minAcademic review pending

Showing a whole collection, not just one value

A JList or JTable displays an entire ArrayList-like collection at once, and a JScrollPane handles content too big for the visible area — without you writing any scrolling logic by hand.

Choose explanation

After this lesson

You should be able to

  • Display a collection of values in a JList or JComboBox.
  • Explain what a JScrollPane does, and why you rarely implement scrolling by hand.
  • Describe what a JTable displays and how it relates to a 2D structure.
01

JList and JComboBox: a collection, shown directly

Where a JLabel shows one fixed piece of text, a JList shows an entire array or collection of items at once, letting the user pick one or several. A JComboBox does the same job in a dropdown form, showing one selected item while the rest stay hidden until clicked. Both take an array or a collection directly in their constructor — the same String[] or ArrayList<String> you already know how to build.

This is the direct payoff of Unit IV's ArrayList lesson: once your data lives in a collection, showing it in the interface is often a one-line call, not a loop you write to draw each item yourself.

Passing an array straight to the component that displays it
String[] subjects = { "Math", "Physics", "Chemistry" };
JList<String> subjectList = new JList<>(subjects);      // shows all three at once

JComboBox<String> subjectDropdown = new JComboBox<>(subjects); // one visible, rest hidden
02

Scroll panes and tables: too much content, handled for you

A list with a hundred items will not fit on screen at once. Wrapping any component in a JScrollPane adds scrollbars automatically whenever the content is bigger than the visible area — you never write pixel-tracking or scroll-position logic yourself, the same abstraction principle from Unit I, now applied to the visible screen.

A JTable displays genuinely two-dimensional data — rows and columns, like a spreadsheet — built from a 2D array or a specialised model object. Conceptually, a JTable is doing for a 2D array what JList does for a 1D one: turning a data structure you already know how to build into something the user can see and interact with directly.

Try it yourself

Build a JList showing five course names. Wrap it in a JScrollPane and add that scroll pane to a frame — not the raw list — so the list becomes scrollable if the window is made small.

Need a hint?

The scroll pane's constructor takes the list, and it is the scroll pane, not the list directly, that gets added to the frame.

Check the worked solution

Adding the raw JList directly to the frame gives no scrolling at all — the scrolling behaviour comes specifically from wrapping it in JScrollPane first and adding that wrapper instead, exactly mirroring how a container in Unit IV holds and positions the components placed inside it.

String[] courses = { "CS105ES", "CS205ES", "CS301PC", "CS303PC", "CS305PC" };
JList<String> courseList = new JList<>(courses);
JScrollPane scrollPane = new JScrollPane(courseList);

frame.add(scrollPane);   // add the scroll pane, not courseList directly

Quick check

Why must a JList be wrapped in a JScrollPane and that JScrollPane added to the frame, rather than adding the JList directly?

Select an answer to check your thinking.

Why this lesson exists

Syllabus mapping

JList, JComboBox, Tabbed Panes, Scroll Panes, Trees, and Tables.

Maps to course outcome CO4.