title: Enforcing an implicit minimum size on a JDialog?
body: <h2>Question</h2>
  
  <p>I have an application-modal <code>JDialog</code> which I want to have an enforced minimum size (i.e. the OS prevents the user from making the window any smaller than the minimum size).  But the window should still be resizable (i.e. <code>setResizable(false)</code> is not an option).  Preferably, the enforced minimum size should be implicitly defined (i.e. dynamically determined by the minimum sizes of the child components, and not a fixed size set explicitly using <code>setMinimumSize</code>)</p>
  
  <p>The problem I've run into is that it seems if I don't explicitly set a minimum size on the <code>JDialog</code> via <code>setMinimumSize</code>, the OS will not enforce it.  So even though the <code>JDialog</code> implicitly knows the correct minimum size to use at any given time, that size won't be enforced unless I call something like <code>dlg.setMinimumSize(dlg.getMinimumSize())</code>.  See the SSCCE below.</p>
  
  <p>In many cases, explicitly setting the minimum size just before opening the dialog is sufficient.  However, sometimes I have a situation where the implicit minimum size can change while the dialog is open, e.g. a child component may be dynamically added or removed from the dialog.  And if I set an explicit minimum size on the dialog, that size may not be sufficiently large for the new layout.</p>
  
  <p>Is there any way to configure a <code>JDialog</code> to enforce the implicit minimum size of the dialog?  If not, is there some other elegant way to enforce a dynamically changing minimum size on an open dialog?</p>
  
  <h2>SSCCE</h2>
  
  <p>In this SSCCE, the goal is to create a dialog that cannot be made any smaller than 300x300 pixels.  This goal is only met if the minimum size is explicitly set.</p>
  
  <pre><code>public class SSCCE {
      public static void main(String[] args) {
          JDialog dlg = new JDialog(null, ModalityType.APPLICATION_MODAL);
          dlg.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
  
          JPanel pnl = new JPanel(new BorderLayout());
          pnl.add(new JLabel("300x300"), BorderLayout.CENTER);
  
          // The panel is given a hard-coded minimum size just to make the
          // problem easier to see
          pnl.setMinimumSize(new Dimension(300,300));
  
          dlg.add(pnl);
  
          System.out.println("Dialog minimum size: " + dlg.getMinimumSize());
          // Minimum size is not enforced by the OS without this call
          dlg.setMinimumSize(dlg.getMinimumSize());
          System.out.println("Dialog minimum size: " + dlg.getMinimumSize());
  
          dlg.pack();
          dlg.setVisible(true);
      }
  }
  </code></pre>
  
  <p>With explicit <code>setMinimumSize</code> call:</p>
  
  <p><a href="https://i.stack.imgur.com/bTyhB.png" rel="nofollow"><img src="https://i.stack.imgur.com/bTyhB.png" alt="Properly sized 300x300 JDialog"></a></p>
  
  <p>Without explicit <code>setMinimumSize</code> call:</p>
  
  <p><a href="https://i.stack.imgur.com/iDe79.png" rel="nofollow"><img src="https://i.stack.imgur.com/iDe79.png" alt="Improperly sized JDialog"></a></p>
  
  <p>Output in both cases:</p>
  
  <blockquote>
    <p>Dialog minimum size: java.awt.Dimension[width=300,height=300]<br>
    Dialog minimum size: java.awt.Dimension[width=300,height=300]</p>
  </blockquote>
  
  <p>For what it's worth - I observed this on Windows 8.1 with jdk1.8.0_91.</p>