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 EmptyBorder
– which can be a pain as well since borders don't overlap and you may end up with too much space between two controls1 – so you may need to leave out one side of the border.
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 is essentially a platform issue, since Microsoft, Apple, Gnome and others all have different guidelines on that topic.
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.
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 GridBagLayout
is great from this perspective, but horrible if you still need to make changes to the UI. Constructing the UI from several JPanel
s with appropriate layout managers vice versa, then.
In short: How can I make sure that controls are spaced nicely and fitting for the LAF, without too much of a hassle? Maybe I'm just overlooking something here – with the sheer size of Swing that's not unlikely.
1 At least for me it was a problem – maybe there is 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 – between 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).