Skip to main content

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.

Comments

  1. Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging. If anyone wants to become a Java developer learn from Java Training in Chennai. or learn thru Java Online Training in India . Nowadays Java has tons of job opportunities on various vertical industry.

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

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

    ReplyDelete
  4. Explore our heavy duty pulley at TrekkersPK, built for strength and stability during demanding climbing and rescue tasks. This aluminum swing pulley guarantees top performance and safety. heavy duty pulley

    ReplyDelete

Post a Comment

Popular posts from this blog

Evolution of Java Programming Language

Evolution of Java: First of all, The Java programming language and it's tools that we currenly using is not what actually expected. This language started as a project to give solutions for embedded devices, mobile phones and other portable peripherals.  Initially, the Java was called Oak.  Why it is Oak first..? and Why it transformed to Java..? The team of five technocrats including Dr. James Gosling and also known as Dr. Java, together working on a project, which expected to give solutions on embedded devices, portable smart devices and TV Set top Boxes etc. at Sun Micro Systems.  In the process of achieving this solution, at their breaktime, team used to relax with a coffee by having a glimpse on a scenery of Oak tree next to their facility.  Once the project has come to conclusion and in the process of giving a title to it, they noticed the Oak trees [Always observing them in their break time], they named this programming language as Oak. Oak Logo Why it transfor...

Agile Methodology with SCRUM Framework Basics

Software development activities can be managed and taken care with different life cycle models. These life cycle models has became legacy since few years. Few of the available Software Development Life Cycle Models in short SDLC are Win-Win Model and Waterfall Model etc. These traditional models has different phases of development.           1. Requirements            2. Analysis            3. Design            4. Implementation            5. Test           6. Documentation  and 7. Maintenance  In SDLC, the above stages are freezed one after the other. If developer is in Design phase and realized that there could be a possibility of change in requirements, then it is not possible to go back one phase and fix in Requirement phase.  These scenarios and use cases has brough...