- 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
Java Program to set Horizontal Alignment of content in a JTextField
The content in the JTextFile is by default left aligned, but you can change it using the setHorizontalAlignment() method. Let’s say the following is our JTextField −
JTextField emailId = new JTextField(20);
Now, we will set the horizontal alignment and set the alignment to the right for the JTextPane −
emailId.setHorizontalAlignment(JTextField.RIGHT);
The following is an example to set horizontal alignment of content in a JTextField −
package my; import java.awt.FlowLayout; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JTextField; import javax.swing.SwingConstants; public class SwingDemo { public static void main(String[] args) throws Exception { JFrame frame = new JFrame("Demo"); JLabel label; frame.setLayout(new FlowLayout()); label = new JLabel("Email-Id : ", SwingConstants.LEFT); JTextField emailId = new JTextField(20); emailId.setHorizontalAlignment(JTextField.RIGHT); frame.add(label); frame.add(emailId); frame.setSize(550,250); frame.setVisible(true); } }
Output
- Related Articles
- Can we save content from JTextField to a file in Java?
- How to set the horizontal alignment of text with JavaScript?
- How to set the alignment of the JLabel content along the X axis in Java?
- How to set the alignment of the JLabel content along the Y axis in Java?
- Java Program to set alignment for text in JLabel
- How to set the horizontal alignment of an element with JavaScript?
- 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 X axis on the left 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
- How to set the alignment of labels in horizontal bar plot to left in R?
- How to set the vertical alignment of the content in an element with JavaScript?
- How to set vertical alignment for a component in Java?
- CSS Central, Horizontal and Vertical Alignment
- Java Program to get the content of a directory

Advertisements