Reputation: 1553
I'm trying to have my Swing components not scale either vertically and scale horizontally, or the other way around (In other words, I only want them to have one axis of freedom. They can either scale horizontally in some cases, or vertically in others).
Is there any elegant way of doing this, without adding a multitude of JPanels?
LE: I'd rather not make any significant modifications to the already made layout that I've received.
I'm hoping for a magical, simple fix to this issue, like JTextField.don'tScaleVerticallyIdiot();
Upvotes: 0
Views: 2687
Reputation: 5435
You didn't mention which LayoutManager
that you are currently using.
Personally, I would use GridBagLayout
for what you are trying to achieve.
Upvotes: 2
Reputation: 7136
I want it to be a single row as height and scale horizontally as much as I drag the frame, but without adding multiple JPanels.
I think BoxLayout
with a PAGE_AXIS
can do that.
Upvotes: 1
Reputation: 117665
I think you can achieve that with setting BorderLayout as a LayoutManager of the container, so:
Upvotes: 2
Reputation: 40861
You need to use an appropriate layout manager.
The two most powerful are MigLayout and FormLayout. I personally prefer MigLayout because it's easier to use directly, whereas FormLayout works well only with a window designer like WindowBuilder.
Both have similar feature characteristics.
Using MigLayout, you can specify if a component is allowed to grow on a specific axis, and if so, what kind of weight it should be provided (as opposed to extra space going to other components).
Upvotes: 3