- 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 ceate right justified JTextField in Java?
To create right justified JTextField, set the alignment to be RIGHT. Here, we will be using the setHorizontalAlignment() method as well and within that the alignment would be set.
Create a JTextField −
JTextField emailId = new JTextField(20);
Now, align it to the right −
emailId.setHorizontalAlignment(JTextField.RIGHT);
The following is an example to create right justified JTextField −
Example
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("Enter emailid..."); JLabel label; frame.setLayout(new FlowLayout()); label = new JLabel("TextField (Right Justified)", SwingConstants.LEFT); JTextField emailId = new JTextField(20); emailId.setHorizontalAlignment(JTextField.RIGHT); frame.add(label); frame.add(emailId); frame.setSize(550,250); frame.setVisible(true); } }
This will produce the following output. Now, if yiu will tyoe any text in the component, it would begin from the right −
Output
- Related Articles
- How to arrange components in a Flow to be right-justified in Java?
- How to set the text of the JLabel to be right-justified and vertically centered in Java?
- How to set the content of the label to be right-justified and top-aligned in Java?
- How can we add padding to a JTextField in Java?
- How to ceate MySQL Trigger before Insert?
- How can we implement a rounded JTextField in Java?
- Java Program to set the content of the JLabel to be right-justified and bottom-aligned
- How to get a space-padded string with the original string right-justified in Python?
- How can we make JTextField accept only numbers in Java?
- How to arrange components in a FlowLayout to be left-justified in Java?
- Can we save content from JTextField to a file in Java?
- Java Program to set Horizontal Alignment of content in a JTextField
- How to read an input value from a JTextField and add to a JList in Java?
- How can we limit the number of characters inside a JTextField in Java?
- How can we implement cut, copy and paste functionality of JTextField in Java?

Advertisements