GridLayout as Layout Manager in Java

This tutorial is about the GridLayout as Layout Manager in Java. A GridLayout is a layout where it has a grid within a component. This layout ignores the sizes of the component and resizes them fit the cell's dimension of the grid. So, now let's start this tutorial! 1. Open JCreator or NetBeans and make a java program with a file name of gridLayout.java. 2. Import the following packages:
  1. import java.awt.*; //used to access of GridLayout class
  2. import javax.swing.*; //used to access the JFrame and JButton class
3. Initialize your variable in your Main, variable frame for JFrame, and 6 buttons namely b1,b2,b3,b4,b5, and b6 as JButton.
  1. JFrame frame = new JFrame("GridLayout as Layout Manager");
  2. JButton b1, b2, b3, b4, b5, b6;
  3.  
  4. b1 = new JButton("Button1");
  5. b2 = new JButton("Button2");
  6. b3 = new JButton("Button3");
  7. b4 = new JButton("Button4");
  8. b5 = new JButton("Button5");
  9. b6 = new JButton("Button6");
4. To have a GridLayout as the layout manager, we will use the setLayout method of the frame.
  1. frame.getContentPane().setLayout(new GridLayout(2,3));
As you can see there are two parameters of GridLayout class. The 2 indicates the number of rows and the 3 indicates the column of the grid. When we create a GridLayout object, you indicate the number of rows and columns, and the container will be divided to that grid accordingly. Then add the buttons into the frame using the add method.
  1. frame.getContentPane().add(b1);
  2. frame.getContentPane().add(b2);
  3. frame.getContentPane().add(b3);
  4. frame.getContentPane().add(b4);
  5. frame.getContentPane().add(b5);
  6. frame.getContentPane().add(b6);
5. Lastly, set the size, visibility, and the close operation of the frame. Have this code below:
  1. frame.setSize(500, 400);
  2. frame.setVisible(true);
  3. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Output: output Here's the full code of this tutorial:
  1. import java.awt.*; //used to access of GridLayout class
  2. import javax.swing.*; //used to access the JFrame and JButton class
  3.  
  4. public class gridLayout{
  5. public static void main(String[] args) {
  6.  
  7. JFrame frame = new JFrame("GridLayout as Layout Manager");
  8. JButton b1, b2, b3, b4, b5, b6;
  9.  
  10. b1 = new JButton("Button1");
  11. b2 = new JButton("Button2");
  12. b3 = new JButton("Button3");
  13. b4 = new JButton("Button4");
  14. b5 = new JButton("Button5");
  15. b6 = new JButton("Button6");
  16.  
  17. frame.getContentPane().setLayout(new GridLayout(2,3));
  18.  
  19.  
  20. frame.getContentPane().add(b1);
  21. frame.getContentPane().add(b2);
  22. frame.getContentPane().add(b3);
  23. frame.getContentPane().add(b4);
  24. frame.getContentPane().add(b5);
  25. frame.getContentPane().add(b6);
  26.  
  27. frame.setSize(500, 400);
  28. frame.setVisible(true);
  29. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  30. }
  31. }
For more inquiries and need programmer for your thesis systems in any kind of programming languages, just contact my number below. Best Regards, Engr. Lyndon Bermoy IT Instructor/System Developer/Android Developer/Freelance Programmer Mobile: 09488225971 Landline: 826-9296 E-mail:[email protected] Add and Follow me on Facebook: https://www.facebook.com/donzzsky Visit and like my page on Facebook at: https://www.facebook.com/BermzISware

Add new comment