Source Code : Modify horizontal alignment of text field at runtime
Java Is Open Source Programming Language You Can Download From Java and Java Libraries From http://www.oracle.com.
Click Here to download
We provide this code related to title for you to solve your developing problem easily. Libraries which is import in this program you can download from http://www.oracle.com.
Click Here or search from google with Libraries Name you get jar file related it
Modify horizontal alignment of text field at runtime
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JTextField;
public class Main {
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);
}
});
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();
}
}
Thank with us