I have a simple problem here in Java Swing. I simplified my code to the following snippet. I am not sure how I can minimize the gap size between the horizontal JSeparator with the next JTextField, as current code generates a huge gap between the two.
GroupLayout layout = new GroupLayout(jPanel1);
jPanel1.setLayout(layout);
layout.setHorizontalGroup(layout.createParallelGroup()
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createSequentialGroup()
.addComponent(button)
))
.addComponent(jSeparator)
.addComponent(jTextField)
);
layout.setVerticalGroup(layout.createSequentialGroup()
.addComponent(button)
.addComponent(jSeparator)
.addComponent(jTextField)
);
And also in general, how can I control the gap size to any integer represented value, instead of using the addPreferredGap
?
Thank you.
Okay, this is the window generated from the code posted above:
You can see the space between the JSeparator and JTextField is very wide.
in the vertical layout, add the separator in the following way:
Absent your sscce, the problem appears to be in the code you don't show. The parent container's layout or
pack()
may be involved. Note that the default layout ofJFrame
isBorderLayout
; the default position isCENTER
. Here's an sscce with which to compare your code.Addendum: Commenting that the parent of your
GroupLayout
panel is anotherJPanel
, you asked the following,Yes, give the enclosing
JPanel
a suitable layout, e.g.GridLayout
as shown below. The latter behaves much like theBorderLayout.CENTER
of theJFrame
in this regard.