Concepts / Creating Custom JPanel Components

Creating Custom JPanel Components

The main panel of the card game uses a BorderLayout, with the drawing surface in CENTER and the subpanel of three buttons in SOUTH.

  • Programming

Why a Window Needs a Design Plan

Imagine a card game where you look at one playing card and must guess whether the next card will be higher or lower in value. Aces count as the lowest card in this game. Three correct guesses win the round, and a single wrong guess ends it. A text-based version of this game only needs the console. A graphical version needs a window that shows the cards, displays a message to the player, and offers buttons for making predictions and starting a new game.

Building that window is a design problem before it is a coding problem. You have to decide what goes where, which object owns the drawing, and which object reacts when the player presses a button. This article works through those three decisions in order, starting with the layout of the main window. Before reading on, pause and ask yourself: the window has a large area for the cards and message, and a row of three buttons. Which part of the window should the large area occupy?

Five Regions of a BorderLayout

A BorderLayout divides its container into five regions named NORTH, SOUTH, EAST, WEST, and CENTER. You place a component into one of these regions, and the layout positions it there. You do not have to fill every region. Any region you leave unused stays empty and contributes no visible content, so the design does not need a component for it.

NORTHemptyWESTemptyCENTERdrawing surface (CardPanel)EASTemptySOUTHsubpanel of three buttons
Where does each of the five regions sit inside the main panel, and which component goes in which region?

Read the diagram as a frame around the window. NORTH runs along the top, SOUTH along the bottom, WEST down the left side, EAST down the right side, and CENTER fills whatever room remains in the middle. In the card game, only two of these five regions are used. The drawing surface goes in CENTER, and the subpanel holding the buttons goes in SOUTH. NORTH, EAST, and WEST are left empty.

Predicting Where the Cards Go

What do you think happens?

In the card game's main panel, which region does the drawing surface occupy?

  • NORTH

  • SOUTH

  • CENTER

  • WEST

Reveal answer
Answer:

CENTER

The drawing surface, which draws the cards and message, is placed in CENTER. The subpanel of three buttons is placed in SOUTH.

Worked example

Placing a new panel in a BorderLayout

A designer wants to add a status label above the card area and keep the three buttons at the bottom. Which regions should hold each component?

  1. 1
    Identify the components

    There is a status label, a drawing surface for the cards and message, and a row of three buttons grouped in a subpanel.

  2. 2
    Place the drawing surface

    The large area is the main content, so it goes in CENTER, as the card game does.

  3. 3
    Place the button subpanel

    The button row goes in SOUTH, following the card game's design.

  4. 4
    Place the status label

    The label sits above the card area, so it goes in NORTH. This is a design choice made for this example, not a rule drawn from the card game.

  5. 5
    Check the unused regions

    EAST and WEST have no components, so they stay empty and contribute no visible content.

Result

NORTH holds the status label, CENTER holds the drawing surface, SOUTH holds the button subpanel, and EAST and WEST remain empty.

Two JPanel Classes With Different Jobs

The card game is built from two JPanel classes that play different roles. The outer panel is HighLowGUI, which is a subclass of JPanel. Its constructor creates the other components, sets up event handling, and lays out the components in the BorderLayout. The inner panel is a nested class named CardPanel, which is also a subclass of JPanel. CardPanel is the drawing surface, and it sits in the CENTER region of the outer panel.

HighLowGUImain JPanel subclassCardPaneldrawing surface in CENTERThree buttonspredictions and new gameButton subpanelin SOUTHCENTERSOUTHcontains
Which panel contains which component, and how does the button subpanel sit inside the main panel's layout?

Notice that the button subpanel is a child of the main panel's layout, and the three buttons live inside that subpanel. The main panel does not place the buttons directly; it places the subpanel in SOUTH. This nesting keeps the button row together as one unit inside the larger window.

Why the Drawing Surface Gets Its Own Class

Subclassing JPanel lets you attach behavior to a panel that the standard class does not provide. The card game's drawing surface does not use ready-made components for the cards or the message. Instead, the cards and message are drawn with the graphics context inside the panel's paintComponent() method. A custom subclass is the natural home for that code, because the drawing logic belongs to the same object that is responsible for the display.

AspectHighLowGUI (outer panel)CardPanel (drawing surface)
SuperclassJPanelJPanel
Where it sitsThe main windowCENTER region of the main panel
Main responsibilityBuilds components, sets up event handling, lays out the windowDraws the cards and message in paintComponent()
Relationship to buttonsIts layout holds the button subpanelListens for events from the three buttons

Two JPanel subclasses with separate roles in the card game

Tracing a Button Click

Event handling is the link between a user action and the code that responds to it. When the player clicks a button, the event must reach the object that knows what to do. In this design, the drawing surface listens for events from the three buttons. That means the buttons are not the ones deciding the outcome. The buttons only report that they were pressed, and the listening object takes it from there.

Playerpresses a buttonButtonone of the three in thesubpanelCardPanellistens and decides theresponseclicksreports the press
When a button is clicked, what objects does the event pass through in order, and which object receives it to decide what happens next?
  1. 1
    The player presses one of the three buttons in the SOUTH subpanel.
  2. 2
    The button reports that it was pressed. It does not decide the result of the press.
  3. 3
    The event reaches CardPanel, which listens for events from the three buttons.
  4. 4
    CardPanel is the object that knows what to do with the press, so it is the object that carries the response forward.

Mistakes Learners Make With Layout and Events

  • Common mistake

    Assuming every BorderLayout region must hold a component

    What it looks like

    Leaving NORTH, EAST, and WEST empty and wondering why the window looks wrong

    Why it fails

    An unused region simply contributes no visible content. The card game uses only CENTER and SOUTH.

    Do this instead

    Place components only in the regions the design needs and leave the rest empty.

  • Common mistake

    Treating the buttons as the object that decides the outcome

    Why it fails

    The buttons only report that they were pressed. The source assigns the listening role to the drawing surface.

    Do this instead

    Follow the event to the listening object, which is CardPanel in this design.

  • Common mistake

    Putting the card-drawing code in the main window class without a reason

    Why it fails

    The drawing logic belongs with the display. The card game keeps it inside the CardPanel subclass, separate from the window setup in HighLowGUI.

    Do this instead

    Give the drawing surface its own JPanel subclass and draw inside its paintComponent() method.

  • Common mistake

    Placing the three buttons directly in the main panel instead of a subpanel when the design calls for them to stay grouped

Practice: Plan the Layout

MEDIUM

A new version of the card game adds a score label that should appear at the top of the window. Using the card game's design as a model, decide which BorderLayout region holds the score label, which holds the drawing surface, and which holds the button subpanel. Then explain which object should listen for the button events and why.

Hints
  • The card game places its large drawing area in CENTER and its button row in SOUTH.
  • A label across the top of the window is a natural fit for NORTH.
  • The source says the drawing surface listens for events from the three buttons.

Key Takeaways

  • A BorderLayout divides a container into five regions: NORTH, SOUTH, EAST, WEST, and CENTER. Unused regions stay empty.
  • The card game puts its drawing surface, a CardPanel subclass of JPanel, in CENTER and a subpanel of three buttons in SOUTH.
  • Subclassing JPanel lets the drawing surface own its paintComponent() code, keeping drawing logic with the display and separate from the window setup in HighLowGUI.
  • A button click is reported by the button and received by the listening object, CardPanel in this design; the buttons do not decide the outcome themselves.