# BoxLayout: Abstand zwischen vertikalen Komponenten!



## Guest (22. Jan 2004)

Hi, all.
I've tried this BoxLayout with PAGE_AXIS and X_AXIS alignment, it works, but there is this big gap between the components vertically. The components are self-defined and consists a pair of JLabel and JTextArea.
How do I pack the components tighter vertically i.e. along the Y-Axis ?
TIA 
Here is the stub code for BoxLayout:

```
public class LayoutTest8 extends JApplet implements AdjustmentListener
{ LabeledTextField lblName, lblAddr1, lblAddr2, lblCity;
  public void init()
  { setBackground(Color.lightGray); 
    Container contentPane = getContentPane(); 
    JPanel px = new JPanel();
    ...java...
    JPanel p9 = new JPanel();
    p9.setLayout(new BoxLayout(p9, BoxLayout.Y_AXIS));
    p9.setBorder(BorderFactory.createLineBorder(Color.RED));
      lblName = new LabeledTextField("Name", "Inside a BoxLayout");
      lblAddr1 = new LabeledTextField("Address", "PAGE_AXIS orientation");
      lblAddr2 = new LabeledTextField("Address", "PAGE_AXIS orientation");
      lblCity = new LabeledTextField("City", "PAGE_AXIS orientation");
    p9.add(lblName);
    p9.add(lblAddr1);
    p9.add(lblAddr2);
    p9.add(lblCity);
    px.add(p9);   
    contentPane.add(px);
     ...java...
```
And here is the helper-class for the JLabel and JTextArea pair, called by the aforementioned code to insert the pair into the BoxLayout.

```
public class LabeledTextField extends JPanel
{ private JLabel lbl;
  private JTextField txtFld;
   
  public LabeledTextField(String lblString,
                          Font lblFont,
                          int txtFieldSize,
                          Font txtFont)
  { setLayout(new FlowLayout(FlowLayout.LEFT));
    lbl = new JLabel(lblString, JLabel.RIGHT);
    if (lblFont != null) {lbl.setFont(lblFont);}
    add(lbl);
    txtFld = new JTextField(txtFieldSize);
    if (txtFont != null) {txtFld.setFont(txtFont);}
    add(txtFld);
  }
  public LabeledTextField(String lblString, String txtFieldString)
  { this(lblString, null, txtFieldString, txtFieldString.length(), null);
  }
  public LabeledTextField(String lblString, int txtFieldSize)
  { this(lblString, null, txtFieldSize, null);
  }
  public LabeledTextField(String lblString,
                          Font lblFont,
                          String txtFieldString,
                          int txtFieldSize,
                          Font txtFont)
  { this(lblString, lblFont, txtFieldSize, txtFont);
    txtFld.setText(txtFieldString); 
  }
  public JLabel getLabel() {return(lbl);}
  public JTextField getTextField() {return(txtFld);}
}
```


----------



## acinau (22. Jan 2004)

Sorry, hab ich vergessern, einzuloggern. Stamm von mir...


----------



## acinau (22. Jan 2004)

Inzwischen habe ich verzweifelt mit "struts" versucht,  der abstand zwischen den Komponentent bleibt dasselbe:

```
JPanel p9 = new JPanel();
    p9.setLayout(new BoxLayout(p9, BoxLayout.Y_AXIS));
    p9.setBorder(BorderFactory.createLineBorder(Color.RED));
      lblName = new LabeledTextField("Name", "Inside a BoxLayout");
      lblAddr1 = new LabeledTextField("Address", "PAGE_AXIS orientation");
      lblAddr2 = new LabeledTextField("Address", "PAGE_AXIS orientation");
      lblCity = new LabeledTextField("City", "PAGE_AXIS orientation");
    p9.add(lblName);
      p9.add(Box.createVerticalStrut(1));
    p9.add(lblAddr1);
      p9.add(Box.createVerticalStrut(1));
    p9.add(lblAddr2);
      p9.add(Box.createVerticalStrut(1));
    p9.add(lblCity);
    px.add(p9);   
    
    contentPane.add(px);
```


----------



## Guest (22. Jan 2004)

I've also tried following :

```
p9.add(Box.createRigidArea(new Dimension(0,2)));
p9.add(Box.createVerticalGlue());
```
None of which works, kann jemand da aushelfen bitte?
TIA


----------



## acinau (23. Jan 2004)

Done!
http://java.sun.com/docs/books/tutorial/uiswing/components/formattedtextfield.html
Thank-you for your time.


----------

