JPanel is the general-purpose lightweight container of Java Swing. You use it to group components, give each group its own layout manager, nest groups into larger layouts, and draw custom graphics by overriding paintComponent. Almost every Swing window is built from a tree of JPanel objects inside a JFrame.
Swing is still part of the JDK (javax.swing) and fully supported, although new desktop projects often choose JavaFX or web technologies. Because JPanel is so basic, understanding it explains most of how Swing layouts work.
JPanel extends JComponent, so it is a lightweight (pure Java) component with borders, tooltips, opacity and double buffering by default. Unlike JFrame, it is not a window. It is an invisible rectangle that holds other components and decides how they are arranged.
Its defaults are worth remembering:
| Property | Default |
|---|---|
| Layout manager | FlowLayout |
| Opaque | true (it paints its background) |
| Double buffered | true |
| Preferred size | Computed by the layout manager from the children |
A panel treats related controls as one unit, which you can add, hide, enable or move together.
Calling formPanel.setEnabled(false) does not disable the children, because enabled state is not propagated automatically. Loop over formPanel.getComponents() or keep the controls in a list if you need to toggle them as a group.
Each panel can use its own layout manager, passed to the constructor or set later. Combining layouts across nested panels is the standard way to build complex screens.
The root panel uses BorderLayout, the footer uses FlowLayout aligned right, and the inner button panel uses GridLayout. SwingUtilities.invokeLater starts the UI on the Event Dispatch Thread, which is where every Swing component must be created and changed. frame.pack() sizes the window from the preferred sizes of the panels.
Override paintComponent(Graphics) to draw directly on a panel, and always call super.paintComponent(g) first so the background is cleared. Do not override paint(), which also paints children and borders.
Override getPreferredSize() for custom panels, otherwise layout managers see a size of zero. To redraw after your data changes, call repaint(), never paintComponent directly. Keep the painting method fast and free of object-heavy work, because Swing may call it many times per second.
Listeners are attached to the individual components, not to the panel as a whole, but a panel is a good place to wire them together and expose a clean API. A common pattern is a small subclass per screen section that owns its controls:
Nested panels such as this one can be dropped into a JTabbedPane, a card layout or any other container, which keeps each screen small and testable.
JFrame is a top-level window with a title bar and borders. JPanel is a container placed inside a window or another container.
Common causes are a zero preferred size on a custom panel, a layout manager that does not fit, or adding components after the window is visible without calling revalidate() and repaint().
Either works. JPanel is convenient because it paints a background and works well with layouts. Extend JComponent when you do not want any background painting.
It remains supported in current JDKs and is fine for internal tools and existing applications. For new projects with rich visuals, JavaFX or a web UI may suit better.
JPanel is the basic building block of Swing user interfaces. It groups components, hosts a layout manager, nests inside other panels, and supports custom painting through paintComponent. Build and update panels on the Event Dispatch Thread, give custom panels a preferred size, and structure screens as small panel subclasses.