Layout Managers

Layout Managers
Layout means the arrangement of components within the container. In other way we can say that placing the components at a particular position within the container. The task of layouting the controls is done automatically by the Layout Manager.
The layout manager automatically positions all the components within the container. If we do not use layout manager then also the components are positioned by the default layout manager. It is possible to layout the controls by hand but it becomes very difficult because of the following two reasons.
1. It is very tedious to handle a large number of controls within the container.
2. Oftenly the width and height information of a component is not given when we need to arrange them.
Java provide us with various layout manager to position the controls. The properties like size,shape and arrangement varies from one layout manager to other layout manager. When the size of the applet or the application window changes the size, shape and arrangement of the components also changes in response i.e. the layout managers adapt to the dimensions of appletviewer or the application window.
The list of commonly used controls while designed GUI using AWT.
1. BorderLayout: - The class BorderLayout arranges the components to fit in the five regions: east, west, north, south and center. Each region is can contain only one component and each component in each region is identified by the corresponding constant NORTH, SOUTH, EAST, WEST, and CENTER.
Example: -
import java.awt.*;
import java.awt.event.*;
class AwtLayoutDemo {
private Frame mainFrame;
private Label headerLabel;
private Label statusLabel;
private Panel controlPanel;
private Label msglabel;
public AwtLayoutDemo(){
prepareGUI();
}
public static void main(String[] args){
AwtLayoutDemo awtLayoutDemo = new AwtLayoutDemo();
awtLayoutDemo.showBorderLayoutDemo();
}
private void prepareGUI(){
mainFrame = new Frame("Java AWT Examples");
mainFrame.setSize(400,400);
mainFrame.setLayout(new GridLayout(3, 1));
mainFrame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent windowEvent){
System.exit(0);
}
});
headerLabel = new Label();
headerLabel.setAlignment(Label.CENTER);
statusLabel = new Label();
statusLabel.setAlignment(Label.CENTER);
statusLabel.setSize(350,100);
msglabel = new Label();
msglabel.setAlignment(Label.CENTER);
msglabel.setText("Welcome to TutorialsPoint AWT Tutorial.");
controlPanel = new Panel();
controlPanel.setLayout(new FlowLayout());
mainFrame.add(headerLabel);
mainFrame.add(controlPanel);
mainFrame.add(statusLabel);
mainFrame.setVisible(true);
}
private void showBorderLayoutDemo(){
headerLabel.setText("Layout in action: BorderLayout");
Panel panel = new Panel();
panel.setBackground(Color.darkGray);
panel.setSize(300,300);
BorderLayout layout = new BorderLayout();
layout.setHgap(10);
layout.setVgap(10);
panel.setLayout(layout);
panel.add(new Button("Center"),BorderLayout.CENTER);
panel.add(new Button("Line Start"),BorderLayout.LINE_START);
panel.add(new Button("Line End"),BorderLayout.LINE_END);
panel.add(new Button("East"),BorderLayout.EAST);
panel.add(new Button("West"),BorderLayout.WEST);
panel.add(new Button("North"),BorderLayout.NORTH);
panel.add(new Button("South"),BorderLayout.SOUTH);
controlPanel.add(panel);
mainFrame.setVisible(true);
}
}
Output: -
C:\java\awt>javac AwtLayoutDemo.java
C:\java\awt>java AwtLayoutDemo

2. CardLayout: - The class CardLayout arranges each component in the container as a card. Only one card is visible at a time, and the container acts as a stack of cards.
Example: -
import java.awt.*;
import java.awt.event.*;
class AwtLayoutDemo {
private Frame mainFrame;
private Label headerLabel;
private Label statusLabel;
private Panel controlPanel;
private Label msglabel;
public AwtLayoutDemo(){
prepareGUI();
}
public static void main(String[] args){
AwtLayoutDemo awtLayoutDemo = new AwtLayoutDemo();
awtLayoutDemo.showCardLayoutDemo();
}
private void prepareGUI(){
mainFrame = new Frame("Java AWT Examples");
mainFrame.setSize(400,400);
mainFrame.setLayout(new GridLayout(3, 1));
mainFrame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent windowEvent){
System.exit(0);
}
});
headerLabel = new Label();
headerLabel.setAlignment(Label.CENTER);
statusLabel = new Label();
statusLabel.setAlignment(Label.CENTER);
statusLabel.setSize(350,100);
msglabel = new Label();
msglabel.setAlignment(Label.CENTER);
msglabel.setText("Welcome to TutorialsPoint AWT Tutorial.");
controlPanel = new Panel();
controlPanel.setLayout(new FlowLayout());
mainFrame.add(headerLabel);
mainFrame.add(controlPanel);
mainFrame.add(statusLabel);
mainFrame.setVisible(true);
}
private void showCardLayoutDemo(){
headerLabel.setText("Layout in action: CardLayout");
final Panel panel = new Panel();
panel.setBackground(Color.CYAN);
panel.setSize(300,300);
CardLayout layout = new CardLayout();
layout.setHgap(10);
layout.setVgap(10);
panel.setLayout(layout);
Panel buttonPanel = new Panel(new FlowLayout());
buttonPanel.add(new Button("OK"));
buttonPanel.add(new Button("Cancel"));
Panel textBoxPanel = new Panel(new FlowLayout());
textBoxPanel.add(new Label("Name:"));
textBoxPanel.add(new TextField(20));
panel.add("Button", buttonPanel);
panel.add("Text", textBoxPanel);
Choice choice = new Choice();
choice.add("Button");
choice.add("Text");
choice.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
CardLayout cardLayout = (CardLayout)(panel.getLayout());
cardLayout.show(panel, (String)e.getItem());
}
});
controlPanel.add(choice);
controlPanel.add(panel);
mainFrame.setVisible(true);
}
}
Output: -
C:\java\awt>javac AwtControlDemo.java
C:\java\awt>java AwtLayoutDemo


Free Web Hosting