Layout Managers:
The entire component which we have displays so far has been positioned through the default layout manager. Like we mentioned at the starting of this lesson, a layout manager automatically manages your controls inside a window through using a few type of algorithm. Such as Windows if you have programmed for other GUI environments, then you are accustomed to laying out your controls by hand. Although it is possible to lay out Java controls through hand too, you commonly won't want to, for two major reasons. First, it is extremely tedious to manually layout a huge number of components. Second, A few time the width and height information is not yet available when you required to arrange some control, since the native toolkit component haven't been realized. This is a chicken-and-egg condition. It is pretty confusing to figure out when it is okay to use the size of a produced component to position it relative to another.
Every Container object has a layout manager related along with it. A layout manager is an object of any class which implements the LayoutManager interface. A layout manager is set through the setLayout () method. The default layout manager is used if no call to setLayout() is made. Whenever a container is resized, the layout manager is used to position every of the components inside it.
The setLayout() method has the subsequent common form:
void setLayout(LayoutManager layoutObj)
Above, layoutObj is a reference to the desired layout manager. Pass null for layoutObj if you wish to disable the layout manager and position elements manually. If you do this, you will require to determine the shape and position of every component manually, using the setBounds() method defined through Component. Generally, you will need to use a layout manager.
Every layout manager keeps track of a list of components which are stored through their names. The layout manager is notified every time you add a component to a container. When the container requires to be resized, the layout manager is consulted through its minimumlayoutSize() and preferredLayoutSize() methods. Every component which is being managed through a layout manager holds the getPreferredSize() and getMinimumSize() methods. These return the preferred and minimum size required to show each component. A layout manager will honor these requests if at all possible, therefore maintaining the integrity of the layout policy. You might override these methods for controls which you subclass. Default value is provided or else.
Java has various predefined LayoutManager classes. You could use layout manager which best fits your application.