- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to set horizontal gap between elements in a GridLayout with Java?
Use the setHgap() method to set the horizontal gap between elements in a GridLayout. Let’s say we have a GridLaypout −
GridLayout layout = new GridLayout(2,4);
Set the horizontal gap −
layout.setHgap(25);
The following is an example −
Example
package my; import java.awt.Color; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.Font; import java.awt.GridLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextArea; import javax.swing.WindowConstants; public class SwingDemo { public static void main(String[] args) { JFrame frame = new JFrame("Sections"); JPanel panel = new JPanel(); panel.setBackground(Color.blue); GridLayout layout = new GridLayout(2,4); layout.setHgap(25); panel.setLayout(layout); panel.add(new JButton("Overview")); panel.add(new JButton("Samples")); panel.add(new JButton("Tutorials")); panel.add(new JButton("Support")); panel.add(new JButton("InterviewQA")); panel.add(new JButton("Tools")); panel.add(new JButton("Editors")); frame.add(panel); frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); frame.setSize(500, 300); frame.setVisible(true); } }
Output
- Related Articles
- How to set vertical gap between elements in a GridLayout with Java?
- How to set the gap between the columns with JavaScript?
- Solve unknown gap between elements in Flexbox with HTML5.
- How to create a GridLayout with rows and columns in Java?
- How to create vertical button column with GridLayout in Java?
- How to display JTextArea in the form of a table with GridLayout in Java?
- How to display labels in the form of a 4 column table with GridLayout in Java?
- Java Program to set Horizontal Alignment of content in a JTextField
- What are the differences between GridLayout and GridBagLayout in Java?
- How to display horizontal grid lines in a JTable with Java?
- How to set the horizontal alignment of text with JavaScript?
- How to set horizontal header for a table?
- Set the size of the gap between CSS grid columns
- How to set the horizontal alignment of an element with JavaScript?
- Specify the gap between the columns with CSS

Advertisements