title: Control spacing in Swing
body: <p>Are there guidelines for how much space controls in Swing should have around them? Also there is the question on how exactly to achieve that: Some layout managers have support for gaps between controls; some don't, where you then have to use <code>EmptyBorder</code> – which can be a pain as well since borders don't overlap and you may end up with too much space between two controls<sup>1</sup> – so you may need to leave out one side of the border.</p>
  
  <p>Another problem then are Look and Feels: If I use explicit gaps or borders it may look fine in one LAF but bad in another. For example, Nimbus looks fine with buttons cramped together, since it has extra space around them. The default Windows LAF just crams them together without any extra space. I realize that this <em>is</em> essentially a platform issue, since Microsoft, Apple, Gnome and others <a href="https://stackoverflow.com/questions/1052825">all have different guidelines on that topic</a>.</p>
  
  <p>So if I then put extra space between controls to accommodate for the Windows or Metal LAF, it gets too far spaced out in Nimbus and possibly others.</p>
  
  <p>Then there is the whole problem of how you construct your UI which has severe side-effects on how flexible you are with spacings and borders. Putting everything in one container with <code>GridBagLayout</code> is great from this perspective, but horrible if you still need to make changes to the UI. Constructing the UI from several <code>JPanel</code>s with appropriate layout managers vice versa, then.</p>
  
  <p>In short: <em>How can I make sure that controls are spaced nicely and fitting for the LAF, without too much of a hassle?</em> Maybe I'm just overlooking something here – with the sheer size of Swing that's not unlikely.</p>
  
  <hr>
  
  <p><sup>1</sup> At least for me it was a problem – maybe there <em>is</em> a viable solution. But given the simple case of placing two controls next to each other and you want maybe a five-pixel emptiness around them – <em>between</em> them also only five pixels, not ten. I can't see a way how this is supposed to work out in the general case without creating different borders depending on the writing direction (since left and right are not necessarily where controls go, but apparently always where borders are).</p>