JaiswalTraining

Get the online training



Corejava Servlet Jsp  Php  Hibernate  Ajax Web Service   Ejb2.1 Ejb3.0 Struts Struts2  JPA Spring Ibatis   JSF    JSF2.0  CoreJavaDesignPattern    Jquery  Flex J2EE-Design-Patterns  Jboss7  Maven  Contact Me                                                                                                                                                                        
            IGNOU SMU PTU Project                                           Training                                                                                                                              
              

Contact Us  0091- 9210721663         jaiswaltraining@gmail.com




Corejava
Servlet
Jsp
Php
Hibernate
Ajax
Web Service
Ejb2.1
Ejb3.0
Struts  
Struts2
JPA
Spring
Ibatis
JSF
JSF2.0
CoreJavaDesignPattern
Jquery
Flex
J2EE-Design-Patterns
Jboss7
Maven








Layout Managers

  • Layout management is the process that sets the size and position of components or controls
  • Different Components or controls are Text field, Combo box, List box etc
  • These are placed in a container or in a window
  • Layout Managers are responsible for determining the size and position of component objects within a container
  • Each component in the layout is asked for the space needed to it
  • Then it arranges the components
  • If needed the components are resized
Different methods of layout manager are :

  • getMinimumSize()
  • getPreferredSize()
  • getMaximumSize()
  • When these methods are called they find the space required by the components
  • They report the minimum space needed by a component to display  and uses the optimal size at which it looks best
  • Every component should know the space required by it for the layout manager to arrange the screen.
Two interfaces of ‘java.awt’ package supports to create layout manager
These interfaces are:
  • LayoutManager
    • It is the basic layout manager interface
    • It supports the functionality of laying out the components
  • LayoutManager2
    • It supports layout managers that depend on constraints of the components for placement
    • When a container is created Java automatically creates and assign a default layout manager

Hierarchy Structure of Layout Managers

There are many predefined Layout Managers in Java. Some of them are :






Flow Layout

  • Flow Layout is implemented through java.awt.FlowLayout
  • The default layout manager for all JPanel is Flow Layout i.e.., java.awt.FlowLayout
  • It arranges components in a single row, starting a new row if its container isn’t sufficiently wide
  • If the container is wide the components is placed in center

The Flow Layout has the following constructors



Example:
f=new FlowLayout(f.LEFT, 20, 400)

  • In this f is a object for flowlayout and it take three parameters
    • f.LEFT is an align it may be RIGHT or CENTER
    • 20 is the horizontal gap.
    • 400 is the vertical gap.





Example

Creating a frame in awt with flowlayout


import java.awt.Button;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Label;
import java.awt.TextField;

public class flowlayout  extends Frame
 {
  public flowlayout() {
  
  setVisible(true);
  setSize(500,200);
  FlowLayout flow=new FlowLayout();
  setLayout(flow);
  
  Label name=new Label("Name");
  TextField name1=new TextField(11);
  Label age=new Label("Age");
  TextField age1=new TextField(11);
  Label city=new Label("City");
  TextField city1=new TextField(11);  
  
  add(name);
  add(name1);
  add(age);
  add(age1);
  add(city);
  add(city1);
  }
  
  public static void main(String[] args) {
  
    new flowlayout();
  }
}
Output




Download the code

  • In Flowlayout the components gets arranged according the width.
  • First gets arranged left to right, then top to bottom.
  • In above example if width is reduced then components will get down.
  • if size is set as setSize(300,200) then  frame will be as below



  • if size is set as setSize(300,200)
    then  frame will be as below

 

  • if size is set as setSize(100,200)
    then  frame will be as below




Creating a frame in awt
with flowlayout


01 import java.awt.Button;
02 import java.awt.FlowLayout;
03 import java.awt.Frame;
04 import java.awt.Label;
05 import java.awt.TextField;
06 
07 public class frameflowlayout  extends Frame
08  {
09   public frameflowlayout() {
10   
11   setVisible(true);
12   setSize(200,200);
13   FlowLayout flow=new FlowLayout();
14   setLayout(flow);
15   
16   Button b1=new Button("north");
17   Button b2=new Button("south");
18   Button b3=new Button("east");
19   Button b4=new Button("west");
20   Button b5=new Button("center");
21     
22   add(b1);
23   add(b2);
24   add(b3);
25   add(b4);
26   add(b5);
27     setTitle("my first frame");
28     pack();
29   }
30   
31   public static void main(String[] args) {
32   
33     new frameflowlayout();
34   }
35 }

Output



Download the code