Setting Component Backgrounds
BorderFactory.createEmptyBorder(top, left, bottom, right) creates an empty border that reserves space around the edges of a component.
Why Components Crowd the Edges
When you lay out a Swing interface, components often end up crowded against the edges of their containers. A panel full of buttons can have its buttons touching the panel's boundary, and a text label can sit flush against the edge of its own area. Swing provides static methods in the BorderFactory class to create borders, and one of the most practical of these is createEmptyBorder, which adds an invisible margin of space around a component.
What do you think happens?
A JPanel has a red background. You give it an empty border with the top parameter set to 12 and the other three set to 0. Which part of the panel's background shows the new space?
Reveal answer
Only the top edge of the panel
The empty border reserves room only along the edge whose parameter is nonzero. Nothing is painted in that room, so the panel's red background fills it. The border is invisible as a line, but it still takes up space.
What the Empty Border Actually Creates
BorderFactory.createEmptyBorder(top, left, bottom, right) creates an empty border that reserves space around the edges of a component.
The word empty is literal. The border does not paint a line, a bevel, or a pattern. It only reserves room. Because nothing is drawn in that reserved room, whatever background color the component has will fill the space. This is the key idea to hold onto: the border adds space, and the component's own background shows through that space.
Read the diagram from the outside in. The component's full bounds include the reserved margin. Inside that margin is the empty border space, which paints nothing. The background color is the only thing that appears there. The content area is the smaller region left after the margin is removed, and it is where the component's own material, such as text or nested components, is placed.
Reading the Four Integer Parameters
The method takes four parameters, and they are always given in the same order: top, left, bottom, right. Each parameter is an integer that gives the width of the border along that edge of the component. Because the order is fixed, the first number always controls the top edge, the second the left edge, the third the bottom edge, and the fourth the right edge.
- 12 is the top edge width
- 20 is the left edge width
- 12 is the bottom edge width
- 20 is the right edge width
Each integer is a width, so a larger number reserves more room on that edge. A value of zero on an edge reserves nothing along that side. Because the parameters are positional, swapping two numbers changes the layout even though the call still looks plausible.
Tracing a Panel Before and After the Border
Adding space around a JPanel that holds buttons
A JPanel has a blue background and holds several buttons. The buttons sit right at the panel's edge. Trace what happens to the panel's usable area and background when an empty border is set on it.
- 1Start with the panel as it is
Without a border, the components inside the panel can sit right at the panel's edge. The panel's full bounds and its content area are the same region.
- 2Attach the empty border
Create the border with
BorderFactory.createEmptyBorder and give it the margin you want on each edge. Attach it to the panel so the panel reserves that room. - 3Check the reserved room
The reserved room is empty. Nothing is drawn in it, so the panel's blue background fills the margin on each reserved edge.
- 4Check the usable area
The usable area moves inward. The buttons are now placed inside the content area, which is smaller than the full panel, so they no longer touch the boundary.
The panel keeps its full size and blue background, but its usable area shrinks inward. The buttons gain space from the panel's edges, and the blue background is visible in the reserved margin.
The same reasoning applies to any container. Whatever the container's background is, it shows through the reserved margin, and whatever it holds is laid out within the smaller content area.
Separating Text From a JLabel Edge
The source gives a second common use: a JLabel. Without an empty border, a label does not have any space between its text and the edge of the label. Adding an empty border creates that space, so the text no longer touches the boundary of the label's area.
The text itself does not change. What changes is the content area it is placed in. The empty border reserves room on the edges, so the label's text is laid out within the smaller region and stays clear of the boundary.
Mistakes With Border Parameters
Most errors with createEmptyBorder come from misreading the fixed parameter order or from assuming the border is visible. The items below show the mistakes that most often lead learners to unexpected layouts.
Common mistake Assuming the border is drawn as a visible line
What it looks likeExpecting a dark outline to appear around a panel after calling createEmptyBorder.
Why it failsThe word empty is literal. The border paints nothing and only reserves room.
Do this insteadExpect the component's own background color to fill the reserved space.
Common mistake Swapping the top and left values
What it looks likeWriting createEmptyBorder(20, 12, 20, 12) when you meant 12 on top and 20 on the sides.
Why it failsThe parameters are always in the order top, left, bottom, right, so the first number controls the top edge.
Do this insteadRead each position from the method signature and place the value for each edge in its fixed slot.
Common mistake Expecting the content to move outside the component
What it looks likeThinking the reserved margin pushes the panel's other components outward past the panel's edges.
Why it failsThe border reserves room inside the component's bounds. The content area becomes smaller, not larger.
Do this insteadThink of the content area as shrinking inward while the full component size stays the same.
Common mistake Forgetting that a zero value reserves nothing on that edge
Predicting the Margin
A JLabel with a yellow background has text that touches its left edge. You want to move the text away from the left edge only, leaving the top, bottom, and right edges unchanged. Which parameter position should carry a nonzero value, and which edge of the label will show the yellow background in the new space?
Hints
- Parameters are always in the order top, left, bottom, right.
- Only one edge needs a nonzero width for this change.
- The background shows through any reserved space.
Key Takeaways
- 1
BorderFactory.createEmptyBorder reserves space around a component's edges without drawing anything in that space. - 2The four integers always map in the fixed order top, left, bottom, right, and each one sets the width of the border on that edge.
- 3The component's background color fills the reserved space, so the border adds margin that visibly shows the background.
- 4The usable content area shrinks inward, which is how spacing is added between components and a
JPaneledge or between text and aJLabeledge.
At a glance
BorderFactory.createEmptyBorder(top, left, bottom, right)reserves space around the edges of a component.- The four integer parameters are always in the order top, left, bottom, right, and each gives the border width on that edge.
- The border paints nothing, so the component's background color shows through the reserved space.
- The border is used to space components away from a
JPaneledge and to separate text from aJLabeledge.