- 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
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
Advertisements