Nested Panels

Classic programmed-learning exercises, refreshed for modern Java and presented in the current MrStyner.com portfolio style.

Modern Java noteThis archive has been refreshed for Java 25 LTS. Core language concepts remain useful, while outdated setup instructions and browser-era Java are labeled or replaced. Java 26 is the current feature release; Java 25 is used here as the stable teaching baseline.
go to previous page   go to home page   go to next page

Answer:

Yes.

Nested Panels

[frame with 6 text fields]

JPanels are both components and containers. As a component they can be placed inside containers (such as JPanel) and as a container they can contain other JPanels. Each panel has its own layout manager which may be any of several types. This example uses FlowLayout for all panels.

[nested panels]

The illustration above shows how each label and text field pair has been placed into its own panel (shown with dotted lines), and how those panels have been placed into two other panels (shown with solid lines). Then those two panels have beed added to the frame. The size of the frame was set so that the layout looks nice.

QUESTION 10:

Was it necessary to use the two panels that contain panels for this layout?