Bober02
Bober02

Reputation: 15331

Java swing - fill size

Is there a way to set, when specifying height and width of a component in Swing, that a given object should fill the available vertical/horizontal space? I.e. I would like to say setSize(*, 100) to allow horizontal fill.

Upvotes: 2

Views: 5379

Answers (2)

Robin
Robin

Reputation: 36601

You can call setSize with whatever you want, but in the end it is the LayoutManager of the parent Container which decides what position and what size each component gets.

So by using the correct LayoutManager you can typically succeed in letting a component take all the available width. As you can see in the BorderLayout part of the Visual guide to LayoutManagers tutorial the PAGE_START and PAGE_END take all the available width.

By specifying the height as preferred size you will obtain the desired result

Upvotes: 3

Tom
Tom

Reputation: 44821

A BorderLayout is probably what you'll want to use, it allows you to set components to North, West, South, East and center.

So for horizontal fill use NORTH, for vertical fill use WEST and for both CENTER.

Upvotes: 0

Related Questions