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
JPanels 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).
These kind of guidelines are both platform- and look&feel- dependent.
You can use javax.swing.LayoutStyle utility methods to find out what spacing should be used between a component and its embedding panel, or between two components.
However this is quite cumbersome.
You may consider using a
LayoutManager that directly supports that for you. Some have suggested MigLayout, you could also take a look at DesignGridLayout which makes component spacing totally transparent to the developer.
With such LayoutManagers, you should NEVER set any dimensions or insets on your own but let them perform the work for you.
Additionally, the choice of the right PLAF is important, because spacing can change depending on the resolution of your monitor (you may have ugly behavior with Hi-DPI screens if they are not supported by your selected PLAF).
I can't enumerate which PLAFs are resolution-independent (ie know how to handle HiDPI devices), but Substance is, for sure, one of them, and a very good PLAF in general.
I solved this problem by using MigLayout, which has platform dependent gaps. On top of that it provides other platform dependent features such as button sizes and sequences.
I highly recommend it!
For all L&F and all layouts, you can't be sure : it's the purpose of l&f and layout, not yours.
Usualy, in a swing application, developper choose the layout, and user choose l&f.
Layout never change borders (it's part of composant), but they can change insets. So use combination of your layout, border, and insets.
The BoxLayout has a notion of Filler and RigidArea : see Using Invisible Components as Filler.
The SpringLayout has constraint. See How to Use SpringLayout.
The GroupLayout use gaps. You can read How to Use GroupLayout. GroupLayout is very very complicated by hand, but is very very easy with matisse in netbeans.
I sense your question is less about how to space and more about when and how much to space. Sun Oracle's Java Look and Feel Design Guidelines, 4:Visual Design focuses on logical grouping, with sparing use of titled borders. Here's a concrete example from a game.