0
votes

I am trying to implement a simple JDialog where the upper JPanel has a GridBagLayout which is composed by 5 pairs of JLabels: of each pair, first JLabel acts like a header while second contains data received from a web service invokation. Here is an example:

enter image description here

As you can see, this window is too "empty" with too much space. First of all I cannot understand why upper panel is so much bigger than second. In fact, JPanel with "Risultato" title has GridBagLayout too while first panel is in column 0 and row 0, second panel is in column 0 and row 1.

Then, for each JLabel, I call the following methods: setMinimumSize(new Dimension(231, 25)) setPreferredSize(new Dimension(231, 25)) setMaximumSize(new Dimension(231, 25))

Moreover, I set anchors for each GridBagConstraints associated to each JLabel like:

  • GridBagConstraints.FIRST_LINE_START for red;
  • GridBagConstraints.CENTER for green;
  • GridBagConstraints.LINE_START for orange;
  • GridBagConstraints.LINE_END for yellow;
  • GridBagConstraints.LAST_LINE_START for blue;
  • GridBagConstraints.LAST_LINE_END for white;

Finally I also call setWeightX(1.0); for each constraint. I don't know why the effect is this, and why there's so much empty space before and after all JLabels. Is there a way to fix it? So that upper panel adapts to all its component? I cannot write any Java code because my development team is forced to use strange internal framework where we build the layout by writing a xml file where each tag is a container or a component with its right degree of nesting and then, by Reflection API, real Java classes and methods are instantiated and called. Anyway, mechanism should be the same.

UPDATE

Many thanks to Kevin Anderson and I was able to improve the JDialog layout considerably. Moreover, I called setWeightY(0.0) for GridBagConstraints on upper panel container so that it also does not become too big and it it resized to its contained components.

enter image description here

1
Please provide a SSCCE so we can also reproduce your problem. Please also provide how you want to place your components, so we can understand what your problem is.Sergiy Medvynskyy
Unfortunately I cannot, as I have written in my question. Maybe, if possible, I just could have some advice on constraints.SagittariusA
Your labels are in two columns; they all have equal weightX, so GridBagLayout allocates equal width to both columns. But if you give greater weightX, say 10.0, to the labels in the second column; you should be able to make the second column take up more space than the first; you'll get less space between the columns and more at the extreme right side of the panel. .Kevin Anderson
@KevinAnderson, very very kind of yours. Thank you. If you want you can add this comment as answer and I will mark it.SagittariusA

1 Answers

1
votes

Your labels are in two columns and they all have equal weightX, so GridBagLayout allocates approximately equal width to both columns. But if you give greater weightX, say 10.0, to the labels in the second column, make the second column take up relatively much more space than the first. You'll get less space between the columns and more at the extreme right side of the panel.