title: How can I use Swing FlowLayout with fixed width?
body: <p>I'm  doing a prototype for showing some basic functions of a painting program. Now, here is how the window looks like:</p>
  
  <p><img src="https://i.stack.imgur.com/zrgdf.jpg" alt="enter image description here"></p>
  
  <p>The window is compound of a panel with a vertical BoxLayout, inside it, there are a custom image control and another panel with a FlowLayout containing all the buttons.</p>
  
  <p>The problem is that I have to specify the height for the buttons panel (in 100px). If I delete that line, the window looks like this:</p>
  
  <p><img src="https://i.stack.imgur.com/FR5fK.jpg" alt="enter image description here"></p>
  
  <p>But, if I only specify the width, and I write a 0 for the height, I get this:</p>
  
  <p><img src="https://i.stack.imgur.com/8gRCa.jpg" alt="enter image description here"></p>
  
  <p>I want the layouts determine the correct height of the buttons panel by themselves. Here is the code that produces the first image:</p>
  
  <pre><code>public void createAndShowGui(){
      JFrame.setDefaultLookAndFeelDecorated(true);
  
      setTitle("SimplePaint");
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      setResizable(false);
  
      //Main container layout.
      BoxLayout mainLayout = new BoxLayout(getContentPane(), BoxLayout.Y_AXIS);
      getContentPane().setLayout(mainLayout);
  
      drawingPanel = new JPanel();
      drawingPanel.setLayout(null);
      drawingPanel.setPreferredSize(new Dimension(width, height));
  
      TextBox editor = new TextBox();     
      drawingPanel.add(editor);
      editor.setVisible(false);
  
      canvasControl = new DrawableCanvas(editor);
      drawingPanel.add(canvasControl);
      canvasControl.setBounds(0, 0, width, height);
  
      getContentPane().add(drawingPanel);
  
      drawingSurface = canvasControl.getDrawingSurface();
  
      //Buttons layout.
      FlowLayout buttonsLayout = new FlowLayout();
      JPanel buttonsPanel = new JPanel(buttonsLayout);
      buttonsPanel.setPreferredSize(new Dimension(width, 100)); // &lt;-- Not in this way
  
      //Buttons creation...
  
      pack();
  }
  </code></pre>
  
  <p>Thanks in advance.</p>