title: Get preferred size of multi-line flowlayout
body: <p>I can't figure out a way to resize some components in a swing GUI. Some custom labels are being added to a FlowLayout which doesn't behave how they should when resizing the dialog.
  The panel is being built using the jgoodies forms framework.</p>
  
  <p>If using this, where the FlowLayout is being added to xy(3, y)</p>
  
  <pre><code>FormLayout layout = new FormLayout("r:d, 5px, f:d:g", // columns
              "p, p, 5px, p, 5px, p") // rows
  </code></pre>
  
  <p>The FlowLayout expands and a scroll bar shows up<br>
  <img src="https://i.stack.imgur.com/jbLng.png" alt="tags_scroll"></p>
  
  <p>If using</p>
  
  <pre><code>FormLayout layout = new FormLayout("r:d, 5px, f:10:g", // columns
              "p, p, 5px, p, 5px, p") // rows
  </code></pre>
  
  <p>The FlowLayout uses the available space and the items on the second line disappear<br>
  <img src="https://i.stack.imgur.com/5ZHF2.png" alt="tags_vanish"></p>
  
  <p>I'd like to expand the height of each row containing a FlowLayout to the current height of the component. Unfortunately the preferred size always corresponds to the hight for a single row.<br>
  Would another layout be more appropriate? The bold text on the left should be aligned right, followed by the FlowLayout.</p>
  
  <p><a href="https://github.com/taconaut/pms-mlx/blob/master/ps3mediaserver/src/main/java/net/pms/medialibrary/gui/dialogs/fileeditdialog/FileTagsPanel.java#L99" rel="noreferrer">Sources</a></p>
  
  <p><strong>[edit]</strong> After having tried to figure out how to do this for weeks, the actual question can be resumed to this:<br>
  A set of labels is being added to a JPanel. This JPanel should use all available space horizontally (dialogue size minus width of tag name label) and expand as needed vertically. If the height of the JPanel gets bigger then the dialogue, a vertical scroll bar should show up (the horizontal scroll bar is never visible).
  The dialogue can show multiple JPanels which will be shown one after the other (vertically).</p>
  
  <p>Here's an attempt using GridBagLayout and <a href="http://tips4java.wordpress.com/2008/11/06/wrap-layout/" rel="noreferrer">WrapLayout</a>:</p>
  
  <pre><code>public class GridBagLayoutTagPanel extends JPanel {
  private static final long serialVersionUID = -441746014057882848L;
  private final int NB_TAGS = 5;
  
  public GridBagLayoutTagPanel() {
      setLayout(new GridLayout());
  
      JPanel pTags = new JPanel(new GridBagLayout());
      pTags.setBackground(Color.ORANGE);
      GridBagConstraints c = new GridBagConstraints();
      c.ipadx = 5;
      c.ipady = 5;
  
      int rowIndex = 0;
      for (int i = 0; i &lt; NB_TAGS; i++) {
          //add tag name
          JLabel lTagName = new JLabel(String.format("Tag %s:", i));
          lTagName.setFont(lTagName.getFont().deriveFont(Font.BOLD));
          c.fill = GridBagConstraints.NONE;
          c.gridx = 0;
          c.gridy = rowIndex++;
          pTags.add(lTagName, c);
  
          //add tag values
          JPanel pTag = new JPanel(new BorderLayout());
          pTag.add(new JLabel("+"), BorderLayout.LINE_START); //label used to add new tags
          pTag.add(getWrapPanel(), BorderLayout.CENTER); //the list of tag values
          c.fill = GridBagConstraints.HORIZONTAL;
          c.gridx = 1;
          pTags.add(pTag, c);
      }
  
      //JScrollPane sp = new JScrollPane(pTags);
      //sp.setBorder(BorderFactory.createEmptyBorder());
  
      add(pTags);
      }
  
  private static JPanel getWrapPanel() {
     JPanel p = new JPanel(new WrapLayout(FlowLayout.LEFT, 5, 0));
  
     for (int i = 0; i &lt; 50; i++) {
             p.add(new JLabel("t" + i));
     }
     return p;
  }
  
  public static void main(String[] args) {
      JFrame f = new JFrame();
      f.getContentPane().add(new GridBagLayoutTagPanel());
      f.setSize(new Dimension(500, 300));
      f.setVisible(true);
  }
  }
  </code></pre>