
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Create Three Vertical Columns with Equal Buttons in GridLayout
In this article, we will learn how to create a Java program that arranges buttons in three vertical columns with an equal number of buttons using GridLayout. The GridLayout class allows us to arrange components in a grid format, which makes it ideal for creating a uniform layout of buttons.
Problem Statement
Given a set of 12 buttons, we need to create a Java Swing application that organizes these buttons into three vertical columns. Each column should contain an equal number of buttons.Input
No direct input is required from the user. The program will automatically generate 12 buttons labeled as "First Button" through the "Twelfth Button"Output
A window displaying 12 buttons arranged in three vertical columns.
Steps to create three vertical columns with an equal number of buttons
The following are the steps to create three vertical columns with an equal number of buttons ?
- Import necessary classes such as JFrame, JPanel, GridLayout, and JButton.
- Create a JPanel with a GridLayout specifying the number of rows and columns.
- Add 12 buttons to the panel using the add() method.
- Place the panel inside a JFrame to display it on the screen.
- Set the appropriate size for the frame and make it visible.
Java Program to create three vertical columns with an equal number of buttons
The following is an example of creating three vertical columns with an equal number of buttons?
package my; import java.awt.BorderLayout; import java.awt.GridBagLayout; import java.awt.GridLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.border.EmptyBorder; public class SwingDemo { public static void main(String[] args) { JPanel panel = new JPanel(new BorderLayout()); panel.setBorder(new EmptyBorder(2, 3, 2, 3)); JPanel layout = new JPanel(new GridBagLayout()); layout.setBorder(new EmptyBorder(5, 5, 5, 5)); JPanel btnPanel = new JPanel(new GridLayout(5, 2, 5, 5)); btnPanel.add(new JButton("First Button")); btnPanel.add(new JButton("Second Button")); btnPanel.add(new JButton("Third Button")); btnPanel.add(new JButton("Fourth Button")); btnPanel.add(new JButton("Fifth Button")); btnPanel.add(new JButton("Sixth Button")); btnPanel.add(new JButton("Seventh Button")); btnPanel.add(new JButton("Eighth Button")); btnPanel.add(new JButton("Ninth Button")); btnPanel.add(new JButton("Tenth Button")); btnPanel.add(new JButton("Eleventh Button")); btnPanel.add(new JButton("Twelfth Button")); layout.add(btnPanel); panel.add(layout, BorderLayout.EAST); JFrame frame = new JFrame("Demo"); frame.add(panel); frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); frame.setLocationByPlatform(true); frame.setSize(500, 400); frame.setVisible(true); } }
Output
Code Explanation
The code creates a Java Swing application that arranges 12 buttons into a grid of 6 rows and 2 columns using GridLayout. The buttons are added to a panel, which is placed in a JFrame to display the window. The layout ensures the buttons are evenly spaced, and the frame is set to a specific size and made visible.