java - Why should you not use setXXXSize()? -


whenever solutions related layouts, come across people advise against using setminimumsize(), setpreferredsize() , setmaximumsize() methods.

the typical argument is, correct layoutmanager should used instead.

my understanding layoutmanager calculates real width/height of components, looking how space need in order resized correctly. using 3 methods looks reasonable me - example when want component have size , let component take rest.

so why using setxxxsize() discouraged?

there lot of answers on linked duplicate (should avoid use of set(preferred|maximum|minimum)size methods in java swing?).

adding reasons why: few exceptions, if using these methods fine-tuning gui on specific look-and-feel (and system-specific settings, e.g. preferred desktop font, etc.). methods aren't inherently evil, typical reasons using them are. start tuning pixel positions , sizes in layout run risk of gui breaking (or @ minimum, looking bad), on other platforms.

as example of this, try changing application's default look-and-feel. options available on platform, no doubt surprised @ how poorly results can rendered.

so, in name of keeping gui functional , nice-looking on platforms (remember, 1 of major benefits of java cross-platformness), should rely on layout managers, etc., automatically adjust sizes of components renders correctly outside of specific development environment.

all said, can conceive of situations these methods justified. again, aren't inherently evil, make sure aware of high potential complications when use them, , try , think if there look-and-feel-independent solution problems.


Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -