Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Java by (4k points)

Several times I've been criticized for having suggested the use of the following methods:

  1. setPreferredSize
  2. setMinimumSize
  3. setMaximumSize

on Swing components. I don't see any alternative to their use when I want to define proportions between displayed components. I have been told this:

With layouts the answer is always the same: use a suitable LayoutManager

I have searched the web a little bit, but I haven't found any comprehensive analysis of the subject. So I have the following questions:

  1. Should I completely avoid the use of those methods?
  2. The methods have been defined for a reason. So when should I use them? In which context? For what purposes?
  3. What exactly are the negative consequences of using those methods? (I can only think to add portability between systems with different screen resolution).
  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?
  5. If the answer to 4 is "yes", won't this lead to a proliferation of LayoutManager classes which will become difficult to maintain?
  6. In a situation where I need to define proportions between children of a Component (eg, child1 should use 10% of space, child2 40 %, child3 50%), is it possible to achieve that without implementing a custom LayoutManager?

1 Answer

0 votes
by (46k points)

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

Yes, you need to avoid using those methods 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 consider it as an API design accident. Somewhat forced by composite components having special ideas about child sizes. "Imperceptibly", because they should have fulfilled their needs with a custom LayoutManager.

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

Some (unfinished, and regrettably the links are broken due to the movement of SwingLabs to java.net) technical ideas are for instance specified in the Rules (hehe). Ethically, posing tonnes of work onto your impoverished 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 need to implement a new LayoutManager for every little variation on my layout?

Yes, there are LayoutManagers sturdy enough to provide a very good approximation to "all layout requirements". The main three are JGoodies FormLayout, MigLayout, DesignGridLayout. So no, in tradition, you infrequently write LayoutManagers except for simple extremely 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 question 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?

Either of the Big-Three can, I never tried GridBag though (too much trouble for too little power).

Related questions

0 votes
1 answer
asked Nov 20, 2019 in Java by Anvi (10.2k points)
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
asked Nov 19, 2019 in Java by Anvi (10.2k points)

Browse Categories

...