• Layout Managers

    Layout Managers

    Using Layout Managers, the way that you place components on a form in Java is probably different from any other GUI system you’ve used. The way components are placed on a form is controlled not by absolute positioning but by a “Layout Manager” that decides how the components lie based on the order that you add() them. The size, shape, and placement of components will be remarkably different from one layout manager to another. In addition, the layout managers adapt to the dimensions of your applet or application window, so if the window dimension is changed, the size, shape, and placement of the components can change in response.

    JApplet, JFrame, JWindow, and JDialog can all produce a Container  with getContentPane() that can contain and display Component. In Container, there’s a method called setLayout() that allows you to choose a different layout manager. Other classes, such as JPanel, contain and display components directly and so you also set the layout manager directly, without using the content pane.

    1. BorderLayout :-

    The applet uses a default layout scheme : the BorderLayout. Without any other instruction, this takes whatever you add() to it and places it in the center, stretching the object all the way out to the edges.

    This layout manager has the concept of four border regions and a center area. When you add something to a panel that’s using a BorderLayout you can use the overloaded add() method that takes a constant values as its first argument.

    BorderLayout.NORTH(top);

    BorderLayout.SOUTH(bottom);

    BorderLayout.EAST(right);

    BorderLayout.WEST(left);

    BorderLayout.CENTER(fill the middle);

    If you don’t specify an area to place the object, it defaults to CENTER.

    1. FlowLayout :-

    This simply “flows” the components onto the form, from left to right until the top space is full, then moves down a row and continues flowing.

    All components will be compacted to their smallest size in a FlowLayout, so you might get a little bit of surprising behavior.

    1. GridLayout :-

    A GridLayout allows you to build a table of components, and as you add them they are placed left-to-right and top-to-bottom in the grid. In the constructor you specify the number of rows and columns that you need and these are laid out in equal proportions.

    1. GridBagLayout :-

    The GridBagLayout provides you with tremendous control in deciding exactly how the regions of your window will lay themselves out and reformat themselves when the window is resized. it’s also the most complicated layout manager, and quite difficult to understand.

    1. BoxLayout :-

    Because people has so much trouble understanding and working with GridBagLayout, swing also includes the BoxLayout which gives you many of the benefits of GridBagLayout without the complexity, so you can often use it when you need to do hand-coded layouts. BoxLayout allows you to control the placement of components either vertically or horizontally and to control the space between the components using something called “struts and glue”.

    Appreciate my work :Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedInPin on PinterestShare on RedditShare on StumbleUponShare on TumblrDigg thisShare on YummlyShare on VKFlattr the authorBuffer this page

Leave a Reply

Your email address will not be published. Required fields are marked *