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

Saturday 9 July 2011

DesignGridLayout: Simple, yet powerful Layoutmanager for arranging swing components

Swing toolkit comes with few standard Layout Managers where none of them serves the need of arranging components in a way that usually developers require. some of them does, but it takes lot of coding time to achieve it. GridBagLayout is the most flexible layout manager available in swing toolkit but their are so many variables that developer has to look after.

DesignGridLayout can be a useful LayoutManager to arrange components in less time with very small snippet of code and can still get proper alignment. Layouting with DesignGridLayout is as easy as coding with various GUI Builder tools available in varoius IDEs.

Simple program which demonstrates DesignGridLayout:

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import net.java.dev.designgridlayout.DesignGridLayout;
import net.java.dev.designgridlayout.Tag;

public class DesignGridLayoutDemo extends JPanel{

  public DesignGridLayoutDemo () {

      DesignGridLayout layout = new DesignGridLayout(this);
      layout.row().grid().add(new JLabel("Username: ")).add(new JTextField("Enter user name "), 2);

      layout.row().grid().add(new JLabel("Password: ")).add(new JTextField("Enter password "), 2);
      layout.emptyRow();layout.emptyRow();
      layout.row().center().fill().add(new JSeparator());
      layout.emptyRow();layout.emptyRow();
      layout.row().bar().add(new JButton(" Login "), Tag.OK).add(new JButton("Cancel"), Tag.CANCEL);
  }


public static void main(String[] args) throws ClassNotFoundException,
                                InstantiationException,
                                IllegalAccessException,
                                UnsupportedLookAndFeelException {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
     SwingUtilities.invokeLater(new Runnable() {

         @Override
    public void run() {
          JFrame frame = new JFrame("Login Form");
          frame.getContentPane().add(new DesignGridLayoutDemo ());
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

          frame.pack();
          frame.setVisible(true);
         }
     });
  }
}
Output:
                                                            
This project has been hosted in java.net site where we can find Tutorial, download links and feature etc.

3 comments:

  1. This comment has been removed by a blog administrator.

    ReplyDelete
  2. This comment has been removed by a blog administrator.

    ReplyDelete

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...