What is setBounds and how do I use it?

Actually, a Swing component does have multiple dimensions, as:

  • current size – setSize() and setBounds() sets this
  • minimum size – setMinimumSize() sets this
  • preferred size – setPerferredSize() sets this
  • maximum size – setMaximumSize() sets this.

SetBounds is a shortcut for setting current size plus location of the widget if you don’t use any layout manager.

If you use a layout manager, it is the responsibility of the layout manager to lay out your components, taking into account the preferred size you set, and ensuring that the comonent never gets smaller than its minimumSize or bigger than its maximumSize.

In this case, the layoutManager will call setSize (or setBounds), and you can not really control the position or dimension of the component.

The whole point of using a layout manager is to have a platform and window-size independent way of laying out your components automatically, therefore you don’t expect to call setSize from your code.

(Personal comment: There are buggy layout managers, I personally hate all of them and rolled my own, which offers the flexibility of MigLayout without the learning curve.)