3

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: enter image description here

You can see the space between the JSeparator and JTextField is very wide.

Qiang Li
  • 10,593
  • 21
  • 77
  • 148

2 Answers2

3

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 of JFrame is BorderLayout; the default position is CENTER. Here's an sscce with which to compare your code.

Addendum: Commenting that the parent of your GroupLayout panel is another JPanel, you asked the following,

Do you know how to make this work in my situation?

Yes, give the enclosing JPanel a suitable layout, e.g. GridLayout as shown below. The latter behaves much like the BorderLayout.CENTER of the JFrame in this regard.

GroupPanel

import java.awt.EventQueue;
import java.awt.GridLayout;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.JTextField;

/** @see http://stackoverflow.com/questions/6769722 */
public class GroupPanel extends JPanel {

    private final JButton button = new JButton("Start");
    private final JSeparator jSeparator = new JSeparator();
    private final JTextField jTextField = new JTextField(10);

    public GroupPanel() {
        GroupLayout layout = new GroupLayout(this);          
        this.setLayout(layout);
        layout.setAutoCreateGaps(true);
        layout.setAutoCreateContainerGaps(true);
        layout.setHorizontalGroup(layout.createParallelGroup()
            .addComponent(button)
            .addComponent(jSeparator)
            .addComponent(jTextField)
        );
        layout.setVerticalGroup(layout.createSequentialGroup()
            .addComponent(button, GroupLayout.PREFERRED_SIZE,
                GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
            .addComponent(jSeparator, GroupLayout.PREFERRED_SIZE,
                GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
            .addComponent(jTextField, GroupLayout.PREFERRED_SIZE,
                GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
        );
    }

    private static void display() {
        JFrame f = new JFrame("GroupPanel");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setLayout(new GridLayout(1, 0));
        f.add(new GroupPanel());
        f.add(new GroupPanel());
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                display();
            }
        });
    }
}
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • thank you for your reply. I actually did something different from you. I used a JPanel inside the JFrame, while everything else are inside the JPanel. In other words, I set the GroupLayout for JPanel1, as you can see from my code. Do you know how to make this work in my situation? – Qiang Li Jul 21 '11 at 05:07
  • Your case is not really different: the enclosing container needs a suitable layout, as shown above. – trashgod Jul 21 '11 at 13:14
  • aha, I see! Your code is also not working! At least not as I wanted it to be. If you resize the window, you see almost the same effect as in my attached screenshot: there is a big gap between the horizontal separator with the TextField. – Qiang Li Jul 21 '11 at 17:55
  • I'd use the default `FlowLayout` or `BoxLayout` with the desired [filler](http://download.oracle.com/javase/tutorial/uiswing/layout/box.html#filler). – trashgod Jul 21 '11 at 18:04
  • do you mind to modify your code above to make it work? My original question persists and I am not sure how I can solve it. Many thanks. – Qiang Li Jul 21 '11 at 18:14
  • To see the default `FlowLayout`, simply delete `new GridLayout()`; `BoxLayout` is left as an exercise for the reader. As you are using the GUI editor, experiment with different layouts and examine the generated code. – trashgod Jul 21 '11 at 18:25
  • See also [Component Size and Resizability](http://download.oracle.com/javase/tutorial/uiswing/layout/group.html). – trashgod Jul 21 '11 at 19:07
  • did you try resize your window using the code you gave above? It will generate the same window as I posed. :( – Qiang Li Jul 21 '11 at 22:17
  • Yes. I sense you didn't do the steps required [to make a component fixed size](http://download.oracle.com/javase/tutorial/uiswing/layout/group.html). – trashgod Jul 22 '11 at 01:27
2

in the vertical layout, add the separator in the following way:

addComponent(separator, GroupLayout.PREFERRED_SIZE,
             GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
Martin Geisler
  • 72,968
  • 25
  • 171
  • 229