EasyLayoutSourceForge Logo
Introduction
News & Status
Coding guidelines
SourceForge
CVS
Example application
Test applet
Direct Download
SourceForge Download
Sampsa Sohlman
Tutorial
Goal of this tutorial is to explain example application.
When desiging form with EasyLayout, I first has to define grid. For grid I have to define resize percentages for columns and rows. This is done by defining two array of integers.
Note! Sum of all percentage values has to be 100.
JPanel l_JPanel_ContentPane = (JPanel)this.getContentPane();
int[] li_columnsPercentages = { 0, 100, 0 };
int[] li_rowPercentages = { 0, 0, 50, 50 };
Then I have to create EasyLayout component. We also define that gap between grid border and component, which is 3 pixes.
EasyLayout l_EasyLayout =
    new EasyLayout(
        null, null,
       li_columnsPercentages, li_rowPercentages,
       3, 3); // Default gap width and height
Then I create components.
JButton l_JButton_Ok = new JButton("Ok");
JButton l_JButton_Cancel = new JButton("Cancel");
JLabel l_JLabel_Text = new JLabel("Salutation");
JTextField l_JTextField_Text1 = new JTextField("Hello world");
JTextField l_JTextField_Text2 = new JTextField("Good bye");
JTextField l_JTextField_Text3 = new JTextField("");
String[] lS_Items = { "Hello world",
    "Hola Mundo" , "Terve maailma" };
JComboBox l_JComboBox = new JComboBox(lS_Items);
Then I add them to container.
Components can be added any order, but I add first components that has no special parameters. These components rely default gap and their size is grid width - 2 *gap. for these I use simples Position constructor. With Position I specify how I want objects behave in grid. With version 1.1 Constraint object is renamed to Position and new inherited constraint has created for maintain compatiblity.
l_JPanel_ContentPane.add(l_JLabel_Text,
    new Position(0, 0));
l_JPanel_ContentPane.add(l_JButton_Ok,
    new Position(2, 0));
l_JPanel_ContentPane.add(l_JComboBox,
    new Position(1,1));
l_JPanel_ContentPane.add(l_JButton_Cancel,
    new Position(2, 1));
l_JPanel_ContentPane.add(l_JLabel_Text,
    new Position(0, 0));
Next I add Textfield in picture number 2. I want that text field's width is coming from grid, but height is preferred height and it is y-axis positioned at center of grid.
l_JPanel_ContentPane.add(l_JTextField_Text1,
    new Position(1, 0, 1, 1,
   Constraint.FULL, Constraint.CENTER));
Next I add Textfield in picture number 7. I want that component is horizontally spanning 2 cells.
l_JPanel_ContentPane.add(l_JTextField_Text3,
    new Position(1, 3, 2, 1));
Finally I add last text field which is in picture number 6. I want this text field to be preferred size and positioned at center of grid.
l_JPanel_ContentPane.add(l_JTextField_Text2,
    new Position(1, 2, 1, 1, // x, y, x-span, y-span
       Position.CENTER, Position.CENTER, // x aligment, y-aligment
       Position.DEFAULT, Position.DEFAULT)); // xgap and ygap use default
Now we have form layout done.
The result you can see by clicking here.
Author : Sampsa Sohlman