title: Formatting panel contents in Java
body: <p>I have a <code>panel</code>, in which I want the content to displayed vertically. To do that, I'm using this line of code:</p>
  
  <pre><code>rightPanel.setLayout(new BoxLayout(rightPanel, BoxLayout.Y_AXIS));
  </code></pre>
  
  <p>As I'm using <code>textfields</code>, then I would like them to take up one line. But by using the line of code specified above, <code>textfields</code> inside the panel get maximized and when I change the window size, the size of the <code>textfield</code> changes accordingly. I also tried removing it, but then the contents inside the panel get displayed horizontally and won't fit inside the panel.</p>
  
  <p>Here is the image of the result: </p>
  
  <p><img src="https://i.stack.imgur.com/W1tLr.png" alt=""></p>
  
  <p>And here is the code where the content of the panel gets created:</p>
  
  <pre><code>//Creates the form for data and button to save it.
              final TextField name;
              final TextField eMail;
              final TextField dateOfBirth;
              final TextField address;
              JLabel nameLabel = new JLabel("Name:");
              name = new TextField ();
              JLabel eMailLabel = new JLabel("E-mail:");
              eMail = new TextField ();
              JLabel dateOfBirthLabel = new JLabel("Date of birth:");
              dateOfBirth = new TextField ();
              JLabel addressLabel = new JLabel("Address:");
              address = new TextField ();
              rightPanel.add(nameLabel);
              rightPanel.add(name);
              rightPanel.add(eMailLabel);
              rightPanel.add(eMail);
              rightPanel.add(dateOfBirthLabel);
              rightPanel.add(dateOfBirth);
              rightPanel.add(addressLabel);
              rightPanel.add(address);
              rightPanel.add(Box.createRigidArea(new Dimension(20, 20)));
              JButton save = new JButton("Save");
              rightPanel.add(save);
  </code></pre>