• Post Reply Bookmark Topic Watch Topic
  • New Topic

Layout managers  RSS feed

 
Mohnish Sharma
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is a basic question.
What is the need for layout managers? I know that if we use setLayout(null) we can position items on the screen programmatically. Why go through the trouble for using a layout manager, when the results may be sub-optimal.
Thanks
 
Stephanie Grasson
Ranch Hand
Posts: 347
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mohnish,
Here is what RHE has to say on your topic:
"Why Java Uses Layout Managers
There are two reasons why Java's AWT uses layout managers. The first reason is a bit theoretical, and you may or may not find yourself convinced by it. The second reason is thoroughly practical.
The theory lies in the position that precise layout (that is, specification in pixels of each component's size and position) is a repetitious and often-performed task; therefore, according to the principles of object-oriented programming, layout functionality ought to be encapsulated into one or more classes to automate the task. Certainly the layout managers eliminate a lot of development tedium. Many programmers dislike the idea of layout managers at first, but come to appreciate them more and more as tedious chores are eliminated.
The practical reason for having layout managers stems from Java's platform independence. Java components borrow their behavior from the window system of the underlying hardware on which the Java Virtual Machine is running. Thus, on a Macintosh, an AWT button looks like any other Mac button; on a Motif platform, a Java button looks like any other Motif button, and so on. The problem here is that buttons and other components have different sizes when instantiated on different platforms.
For example, consider the button that is constructed by the following line of code:
Button b = new Button("OK");
On a Windows95 machine, this button will be 32 pixels wide by 21 pixels high. On a Motif platform, the button will still be 32 pixels wide, but it will be 22 pixels high, even though it uses the same font. The difference seems small until you consider the effect such a difference would have on a column of many buttons. Other components can also vary in size from platform to platform. If Java encouraged precise pixel-level sizing and positioning, there would be a lot of Java GUI's that looked exquisite on their platform of origin -- and terrible on other hosts."
Hope this helps.
Stephanie
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also consider- what happens if the user decides to resize a frame? With no layout manager, you would have to write code that handles the resize event, and determines a new position for each component (unless you plan to ignore the resize entirely and let users use a scrollbar to get to the appropriate part of the frame if it's been truncated). With a layout manager, this is handled for you automatically in most cases. (Whether it's handled well will depend on how well you chose and set up the layout manager to reflect your needs, of course, but that's part of mastering GUI in Java.)
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!