GroupLayout: components don't laid out as supposed to be?

49 views Asked by At

The components supposed to be laid out like this:

(A)
+-------------------+
| label1 textfield1 |
| label2 textfield2 |
| label3 textfield3 |
+-------------------+

But what are they look like now is:

(B)
+---------------------------------------------------------+
| label1 label2 label3 textfield1 textfield2 textfield3 |
+---------------------------------------------------------+

How to make them look like (A)

SSCCE code:

import java.awt.Dimension;
import javax.swing.GroupLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class GroupLayoutTest extends JPanel
{

   GroupLayout layout;
   JTextField tf_id;
   JTextField tf_name;
   JTextField tf_price;
   JLabel lb_id;
   JLabel lb_name;
   JLabel lb_price;

   public GroupLayoutTest()
   {
      layout = new GroupLayout(this);
      layout.setAutoCreateGaps(true);
      layout.setAutoCreateContainerGaps(true);

      lb_id = new JLabel("id");
      lb_name = new JLabel("name");
      lb_price = new JLabel("price");

      tf_id = new JTextField(5);
      tf_name = new JTextField(5);
      tf_price = new JTextField(5);
      createGUI();
   }

   private void createGUI()
   {
      layout.setHorizontalGroup(
              layout.createSequentialGroup()
              .addGroup(layout.createParallelGroup()
                      .addComponent(lb_id)
                      .addComponent(lb_name)
                      .addComponent(lb_price))
              .addGroup(layout.createParallelGroup()
                      .addComponent(tf_id)
                      .addComponent(tf_name)
                      .addComponent(tf_price))
      );

      layout.setVerticalGroup(
              layout.createSequentialGroup()
              .addGroup(layout.createParallelGroup()
                      .addComponent(lb_id)
                      .addComponent(tf_id))
              .addGroup(layout.createParallelGroup()
                      .addComponent(lb_name)
                      .addComponent(tf_name))
              .addGroup(layout.createParallelGroup()
                      .addComponent(lb_price)
                      .addComponent(tf_price)
              ));
   }

   @Override
   public Dimension getPreferredSize()
   {
      return new Dimension(400, 500);
   }

   public void creatAndShowGUI()
   {
      JFrame frame = new JFrame();
      frame.add(this);
      frame.pack();
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setVisible(true);
   }

   public static void main(String[] args)
   {
      javax.swing.SwingUtilities.invokeLater(new Runnable()
      {

         @Override
         public void run()
         {
            GroupLayoutTest test = new GroupLayoutTest();
            test.creatAndShowGUI();
         }
      });
   }

}
1

There are 1 answers

0
SamYonnou On BEST ANSWER

You set the layout up properly, however you forgot to add it to your panel. So add the line this.setLayout(layout); somewhere in your code and it should work.