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:
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.