James Gosling: idealism, the Internet and Java, Pt I

Friday 6 January 2017

GridLayout: fill the form with gridlayout

Swing controls are of two types they are Containers and components. components are arranged on a containers. example components are JLabel, JButton etc and example containers are JPanel, JFrame and JDialog etc.
these components are arranged on a container with the help of layout managers. Few layout managers available in swing toolkit are

1. FlowLayout [Jframe's Default layout manager]
2. GridLayout
3. BorderLayout [JPanel's Default layout manager]
4. GridBagLayout.
5. Custom Layout manager

The custom layout amanger is useful to design programmer's own layout manager. Usually this is useful when any of the available layout managers does not give the needy result.
apart of these above all layout managers, few third party libraries are also available they are

1. DesignGridLayout
2. Mig Layout etc

Design grid Layout


Gridlayout is one of the most simple and flexible layout managers. layout manager is used to arrange the components on a container with proper margins, gap etc.

Gridlayout is suitable when the number of components are even numbered in each row of the container.


Swing program without any layout manager:


import java.awt.GridLayout;
import java.lang.reflect.InvocationTargetException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

public class GridLayoutExample extends JFrame {

private JLabel label1 = null;
    private JLabel label2 = null;
    private JLabel label3 = null;
    private JTextField tf1 = null;
    private JTextField tf2 = null;
    private JTextField tf3 = null;
    private JButton button1 = null;
    private JButton button2 = null;

    public GridLayoutExample() {
        initComponents();
    }

    private void initComponents() {

        label1 = new JLabel("Name: ");
        add(label1);
        tf1 = new JTextField();
        add(tf1);

        label2 = new JLabel("Qualification: ");
        add(label2);

        tf2 = new JTextField();
        add(tf2);
        label3 = new JLabel("Profile: ");
        add(label3);

        tf3 = new JTextField();
        add(tf3);

        button1 = new JButton("Submit");
        add(button1);
        button2 = new JButton("Cancel");
        add(button2);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       
    }


    public static void main(String[] args) throws InvocationTargetException, InterruptedException {

        GridLayoutExample ex = new GridLayoutExample();

        ex.setSize(300, 200);
        ex.setLocationRelativeTo(null);
        
        SwingUtilities.invokeAndWait(new Runnable() {
            @Override
            public void run() {
                ex.setVisible(true);
            }
        });

    }
}

  Output:





Shown below is a Swing program after adding a GridLayout. 

Replace the initComponents() of the above program with the below method


   private void initComponents() {
    GridLayout gl = new GridLayout();
    gl.setColumns(2);
    gl.setRows(4);
    gl.setHgap(15);
    gl.setVgap(15);

    this.setLayout(gl);

    label1 = new JLabel("Name: ");
    add(label1);
    tf1 = new JTextField();
    add(tf1);

    label2 = new JLabel("Qualification: ");
    add(label2);

    tf2 = new JTextField();
    add(tf2);
    label3 = new JLabel("Profile: ");
    add(label3);

    tf3 = new JTextField();
    add(tf3);

    button1 = new JButton("Submit");
    add(button1);
    button2 = new JButton("Cancel");
    add(button2);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       
}



the output after applying layout manager.....







Popular posts

Demonstration of Java NullPointerException

NullPointerException causes and reasons Since Java is object oriented programming language, every thing is considered to be an object. and t...