Using GridLayout in AWT Example

GridLayout arranges components in Grid view.

import java.awt.*;
import java.awt.event.*;
class GridLayoutDemo extends Frame
{
List l1,l2;
public GridLayoutDemo()
{
// Set the frame properties
setTitle("GridLayout Demo");
setSize(400,400);
setLayout(new GridLayout());
setLocationRelativeTo(null);
setVisible(true);
// Create lists
l1=new List();
l2=new List();
// Add items to lists
l1.add("Apple");
l1.add("Mango");
l1.add("Orange");
l1.add("Pineapple");
l1.add("Guava");
l1.add("Banana");
l2.add("Potato");
l2.add("Carrot");
l2.add("Onion");
l2.add("Spinach");
l2.add("Radish");
// Add lists
add(l1);
add(l2);
// Add item listeners
l1.addItemListener(new ItemListener(){
public void itemStateChanged(ItemEvent ie)
{
setTitle("Fruit = "+l1.getSelectedItem()+"; Vegetable = "+l2.getSelectedItem());
}
});
l2.addItemListener(new ItemListener(){
public void itemStateChanged(ItemEvent ie)
{
setTitle("Fruit = "+l1.getSelectedItem()+"; Vegetable = "+l2.getSelectedItem());
}
});
}
public static void main(String args[])
{
new GridLayoutDemo();
}
}


GridLayoutDemo() : Code illustrating GridLayout is written here
new GridLayoutDemo() : Create the object for the class GridLayoutDemo