- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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 display labels in the form of a 4 column table with GridLayout in Java?
We will displays labels in a label with 5 rows and 4 columns using GridLayout −
JPanel panel = new JPanel(new GridLayout(5, 4, 10, 10));
Use a for loop and loop through 1 to 20 to display 20 labels −
for (int i = 1; i <= 20; i++) { panel.add(new JLabel("Displaying label "+String.valueOf(i))); }
The following is an example to display labels in the form of a 4 column table −
Example
package my; import java.awt.GridLayout; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; public class SwingDemo { public static void main(String[] args) { JFrame frame = new JFrame(); JPanel panel = new JPanel(new GridLayout(5, 4, 10, 10)); for (int i = 1; i <= 20; i++) { panel.add(new JLabel("Displaying label "+String.valueOf(i))); } frame.add(panel); frame.setSize(550, 300); frame.setVisible(true); } }
Output
- Related Articles
- How to display JTextArea in the form of a table with GridLayout in Java?
- How to create vertical button column with GridLayout in Java?
- How to create a GridLayout with rows and columns in Java?
- How to display vertical grid lines in a table with Java?
- How to set vertical gap between elements in a GridLayout with Java?
- How to set horizontal gap between elements in a GridLayout with Java?
- How to display the column names from a table excluding some in MySQL?
- How to display multiple labels in one line with Python Tkinter?
- How to display Y-axis labels with more decimal places in R?
- How to display X-axis labels with dash in base R plot?
- Java Program to display table with columns in the output using Formatter
- MySql how to display the records with latest ID in a table?
- How to round MySQL column with float values and display the result in a new column?
- How to insert only a single column into a MySQL table with Java?
- How to extract the column headers in a table in Selenium with python?

Advertisements