3

Whenever I look for solutions related to layouts, I come across people who advise against using the setMinimumSize(), setPreferredSize() and setMaximumSize() methods.

The typical argument is, that a correct LayoutManager should be used instead.

My understanding is that the LayoutManager calculates the real width/height of the components, by looking for how much space they need in order to be resized correctly. So using the 3 methods looks reasonable to me - for example when you want a component to have only a certain size and let another component always take the rest.

So why is using setXXXSize() so discouraged?

1
  • for example when you want a component to have only a certain size and let another component always take the rest that's the exact task of the LayoutManager, not of the component (the latter can't know its surroundings). All a component itself can know are its own requirements. The other way round, the component itself is the only one which has enough knowledge to advertise/calculate its own size requirements, there's nothing that client code can do to infere those requirements.
    – kleopatra
    Mar 10, 2014 at 17:05

1 Answer 1

4

There are a lot of good answers on the linked duplicate (Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?).

Adding to the reasons why: With few exceptions, if you are using these methods you are probably fine-tuning your GUI to look good on a specific look-and-feel (and with your system-specific settings, e.g. your preferred desktop font, etc.). The methods themselves aren't inherently evil, but the typical reasons for using them are. As soon as you start tuning pixel positions and sizes in a layout you run the risk of your GUI breaking (or at minimum, looking bad), on other platforms.

As an example of this, try changing your application's default look-and-feel. Even just with the options available on your platform, you will no doubt be surprised at how poorly the results can be rendered.

So, in the name of keeping your GUI functional and nice-looking on all platforms (remember, one of the major benefits of Java is its cross-platformness), you should rely on layout managers, etc., to automatically adjust the sizes of your components so that it renders correctly outside of your specific development environment.

All that said, you can certainly conceive of situations where these methods are justified. Again, they aren't inherently evil, just make sure you are aware of the high potential for complications when you use them, and always try and think if there is another look-and-feel-independent solution to your problems.

8
  • 1
    Thanks! This is actually much more understandable than the accepted solution of the linked post. I think I get it now what they mean by "proper LayoutManager".
    – AyCe
    Mar 10, 2014 at 17:06
  • 2
    I never understood why I would need external JARs for some simple apps. Java's built-in LayoutManagers are just fine, but thanks. :)
    – AyCe
    Mar 10, 2014 at 18:43
  • 1
    @AyCe Java's built-in LayoutManagers are just fine actually, they aren't, at least not without a way too steep learning curve and even after mastering that climb none of them gets you completely what you need in everyday layout.
    – kleopatra
    Mar 10, 2014 at 22:18
  • 2
    @kleopatra I disagree. I haven't met a layout yet in 10+ years that I couldn't whip up in short order, by hand, using the standard LMs, and we get some crazy requirements.
    – splungebob
    Mar 10, 2014 at 23:43
  • 1
    Whether a given layout manager is or isn't "fine" of course depends entirely on the situation. If you're trying to lay out components in a single row or column, for example, there's nothing "not fine" about a BoxLayout. The real point is this: Some times I see setXXX being used because for one reason or another the coder either couldn't get layout managers to produce the desired results, or simply felt that using them was too cumbersome. So, I bring up the existence of other layout managers simply as an option to file away if that happens to be the fundamental issue in a given situation.
    – Jason C
    Mar 10, 2014 at 23:48

Not the answer you're looking for? Browse other questions tagged or ask your own question.