Simple GridBagLayout Question

A

AJ

Obviously not simple to me, otherwise I wouldn't be here. But maybe
simple for you. I have a JPanel with a GridBagLayout. This JPanel
should have 2 columns and 2 rows. I would like to ensure that the two
columns always remain the same width, no matter their contents. How
can I do this? As it stands now, if I have text in one, and nothing
in the other column, the column with text is wider than the other one.
However, when they both contain equal text, or both are empty, they
are the same width, which is what I want. Thanks a lot!

AJ
 
A

Andrew Thompson

...I have a JPanel with a GridBagLayout.

..This JPanel
should have 2 columns and 2 rows. I would like to ensure that the two
columns always remain the same width, no matter their contents.

Use a GridLayout for the columns.
 
J

Johan Poppe

AJ skrev:
Obviously not simple to me, otherwise I wouldn't be here. But maybe
simple for you.

Nothing is "simple" with GridBagLayout.
I have a JPanel with a GridBagLayout. This JPanel
should have 2 columns and 2 rows. I would like to ensure that the two
columns always remain the same width, no matter their contents.

I don't think you can do that with a GridBagLayout. (At least, I can't
think of a way.) But you can instead use two 2x1 GridLayouts and a
vertical Box.
 
H

Hal Rosser

I ran into this - had to put something in each grid position. In my case, I
put an empty label.
You could put a panel in each position - set the size of each - and put your
components on the panels - kinda messy.
 
A

Andrew Thompson

You could put a panel in each position - set the size of each - and put your
components on the panels - kinda messy.

That would render this broken
layout even more broken.

Explicitly setting sizes of components
is generally a sign you need to rework
the GUI, ..or scrap it and start over.
 
S

Sam

Obviously not simple to me, otherwise I wouldn't be here. But maybe
simple for you. I have a JPanel with a GridBagLayout. This JPanel
should have 2 columns and 2 rows. I would like to ensure that the two
columns always remain the same width, no matter their contents. How
can I do this? As it stands now, if I have text in one, and nothing
in the other column, the column with text is wider than the other one.
However, when they both contain equal text, or both are empty, they
are the same width, which is what I want. Thanks a lot!

AJ

I would use a Border layout with JSplitPane in the center, divided
vertically, with a JTextArea contained in either side. You can set the
width of the JSplitPane, or just take the default. A benefit to this
approach is that the user can decide on the sizing of the columns,
instead of the omniscient developer.

Sam90
 
A

AJ

Obviously not simple to me, otherwise I wouldn't be here. But maybe
simple for you. I have a JPanel with a GridBagLayout. This JPanel
should have 2 columns and 2 rows. I would like to ensure that the two
columns always remain the same width, no matter their contents. How
can I do this? As it stands now, if I have text in one, and nothing
in the other column, the column with text is wider than the other one.
However, when they both contain equal text, or both are empty, they
are the same width, which is what I want. Thanks a lot!

AJ


I ended up changing my 2x2 layout to a 1x2 layout, since the first row
were just labels describing the contents of the second row. So I gave
the elements in the second row (JTextAreas) titled borders, and
arranged it in a 1x2 GridLayout. It looks and works SO much better,
and it is about 1/4 the code to boot! Thanks for the suggestions!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top