Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BorderLayout - Control height but not width

I am relatively new to Java UI and would appreciate your help with a simple problem that I have.

I have a JPanel of type BorderLayout. The CENTER part contains a combo box.

As the outer frame is resized, the panel resizes (this is desired) and the width of the combo box changes (this is desired too). However, the height of the combobox is also changing as the panel is resized. This is not desired.

How can I ensure that my combo box expands in width but not in height?

I also have a button in LINE_START. I would like this button not to stretch at all.

like image 392
Peter Avatar asked Dec 20 '22 22:12

Peter


2 Answers

Put the combo. into the NORTH of a panel that is then added to the CENTER of the main BorderLayout. Do the same with the button.

like image 185
Andrew Thompson Avatar answered Jan 03 '23 01:01

Andrew Thompson


I would strongly suggest for you to use TableLayout my personal favourite. Since you can layout your elements as if they were in a table, without much hassle.

You can read more about it here.

This link takes you to a full site about it.

I am afraid this layout manager might be slightly outdated, certainly the examples on its page are, but it works great even in 1.7 version of Java.

Look at the example there. The option you would use to make the box to stretch (i.e. fill the available space) is TableLayout.FILL.

like image 22
Boro Avatar answered Jan 03 '23 01:01

Boro