- 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 the alignment of the JLabel content along the X axis on the left in Java
To set the alignment of the label content along the X axis on the left, use the setHorizontalAlignment() method and set the location. Let us first set a label component. We have set the label background color as well so that we can check the alignment of the label’s content properly −
JLabel label = new JLabel("Country "); label.setPreferredSize(new Dimension(190, 100)); label.setOpaque(true); label.setBackground(Color.BLUE); label.setForeground(Color.WHITE);
Now, we will align the label content along the X axis on the left by seeting location as LEFT −
label.setHorizontalAlignment(JLabel.LEFT);
The following is an example to set the alignment of the JLabel content along the X axis on the left −
Example
package my; import java.awt.Color; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.Font; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JTextArea; import javax.swing.WindowConstants; public class SwingDemo { public static void main(String[] args) { JFrame frame = new JFrame("Demo Frame"); frame.setLayout(new FlowLayout()); JLabel label = new JLabel("Country "); label.setPreferredSize(new Dimension(190, 100)); label.setOpaque(true); label.setBackground(Color.BLUE); label.setForeground(Color.WHITE); Font font = new Font("Serif", Font.BOLD, 14); label.setFont(font); label.setHorizontalAlignment(JLabel.LEFT); JTextArea text = new JTextArea(); text.setText("United States of America"); font = new Font("Serif", Font.BOLD, 13); text.setFont(font); frame.add(label); frame.add(text); frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); frame.setSize(500, 300); frame.setVisible(true); } }
Output
- Related Articles
- How to set the alignment of the JLabel content along the X axis in Java?
- Set the alignment of the JLabel content along the X axis on the right in Java
- How to set the alignment of the JLabel content along the Y axis in Java?
- How to set the alignment of the JLabel content along the Y axis on the top in Java
- How to set the alignment of the JLabel content along the Y axis in the bottom with Java
- Set the content of the JLabel to be left-justified and top-aligned in Java
- Java program to set the content of the JLabel to be left-justified and bottom-aligned
- Java Program to set alignment for text in JLabel
- How to set the vertical alignment of the content in an element with JavaScript?
- Java Program to set the content of the JLabel to be right-justified and bottom-aligned
- How to set orientation and split components along x-axis in Java?
- Java Program to set Horizontal Alignment of content in a JTextField
- How to set the alignment of labels in horizontal bar plot to left in R?
- How to shift a graph along the X-axis in matplotlib?
- How to set the angle of skew on the X-axis of a Circle using FabricJS?

Advertisements