J. Doe
J. Doe

Reputation: 33

How do I stop center of BorderLayout from taking up east and west?

I have a program in which I use BorderLayout and the EAST and WEST positions are not filled, though the CENTER one is. As a result, the center component extends to the space where the east and west components would be.

How can I prevent this? I know I could just use a blank JPanel in the east and west positions, but that seems hacky.

Upvotes: 2

Views: 1399

Answers (3)

Dakshinamurthy Karra
Dakshinamurthy Karra

Reputation: 5463

What you need is Box.Filler. It is used to create empty components to fill areas. See https://docs.oracle.com/javase/tutorial/uiswing/layout/box.html#filler

Upvotes: 1

JB Nizet
JB Nizet

Reputation: 691715

If what you want is empty space around your center panel, you can add a border to it:

panel.setBorder(BorderFactory.createEmptyBorder(0, 10, 0, 10);

Upvotes: 2

durron597
durron597

Reputation: 32323

You said:

I know I could just use a blank JPanel in the east and west positions, but that seems hacky.

Why is that hacky? How else would you control the amount of size of the visible space? What if you only want it to be a narrow (say, 10 pixels) blank space? What if you want it to be 100 pixels? What if you want the border to grow and shrink based on the size of the corresponding frame?

Adding some sort of component there (it doesn't have to be a JPanel, though that's not a bad choice) allows you to specify the size of it more effectively, and there's nothing inherently wrong with doing so.

Upvotes: 1

Related Questions