Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?

  1. Should I completely avoid the use of those methods?

    Yes for application code.

  2. The methods have been defined for a reason. So when should I use them? In which context? For what purposes?

    I don’t know, personally I think of it as an API design accident. Slightly forced by compound components having special ideas about child sizes. “Slightly”, because they should have implemented their needs with a custom LayoutManager.

  3. What exactly are the negative consequences of using those methods? (I can only think adding portability between systems with different screen resolution.)

    Some (incomplete, and unfortunately the links are broken due to migration of SwingLabs to java.net) technical reasons are for instance mentioned in the Rules (hehe) or in the link @bendicott found in his/her comment to my answer. Socially, posing tons of work onto your unfortunate fellow who has to maintain the code and has to track down a broken layout.

  4. I don’t think any LayoutManager can exactly satisfy all desired layout needs. Do I really need to implement a new LayoutManager for every little variation on my layout?

    Yes, there are LayoutManagers powerful enough to satisfy a very good approximation to “all layout needs”. The big three are JGoodies FormLayout, MigLayout, DesignGridLayout. So no, in practice, you rarely write LayoutManagers except for simple highly specialized environments.

  5. If the answer to 4 is “yes”, won’t this lead to a proliferation of LayoutManager classes which will become difficult to maintain?

    (The answer to 4 is “no”.)

  6. In a situation where I need to define proportions between children of a Component (for example, child 1 should use 10% of space, child 2 40%, child 3 50%), is it possible to achieve that without implementing a custom LayoutManager?

    Any of the Big-Three can, can’t even GridBag (never bothered to really master, too much trouble for too little power).

Leave a Comment