Example usage for javax.swing JTextField getHorizontalAlignment

List of usage examples for javax.swing JTextField getHorizontalAlignment

Introduction

In this page you can find the example usage for javax.swing JTextField getHorizontalAlignment.

Prototype

public int getHorizontalAlignment() 

Source Link

Document

Returns the horizontal alignment of the text.

Usage

From source file:Main.java

public static void main(String[] args) {

    final JTextField tf = new JTextField("press <enter>", 20);
    tf.setHorizontalAlignment(JTextField.RIGHT);

    tf.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            int old = tf.getHorizontalAlignment();
            if (old == JTextField.LEFT)
                tf.setHorizontalAlignment(JTextField.RIGHT);
            if (old == JTextField.RIGHT)
                tf.setHorizontalAlignment(JTextField.CENTER);
            if (old == JTextField.CENTER)
                tf.setHorizontalAlignment(JTextField.LEFT);
        }/*from  w w  w  .  j a v a 2 s .  c o m*/
    });

    JFrame frame = new JFrame("JTextFieldExample");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setLayout(new java.awt.FlowLayout());
    frame.getContentPane().add(tf);
    frame.setSize(275, 75);
    frame.setVisible(true);
    tf.requestFocus();
}