Aligning the Text in a JTextField Component - Java Swing

Java examples for Swing:JTextField

Description

Aligning the Text in a JTextField Component

Demo Code

import javax.swing.JTextField;

public class Main {
  public static void main(String[] args) {
    // Create a text field with some initial text
    JTextField textfield = new JTextField("Initial Text");

    // Left-justify the text
    textfield.setHorizontalAlignment(JTextField.LEFT);

    // Center the text
    textfield.setHorizontalAlignment(JTextField.CENTER);

    // Right-justify the text
    textfield.setHorizontalAlignment(JTextField.RIGHT);
  }/*from w  w  w .  j  a v a2 s .co  m*/
}

Related Tutorials