java left logo
java middle logo
java right logo
 

Home arrow Java SE Tips arrow javax.swing arrow How to modify horizontal alignment of text field at runtime
 
 
Main Menu
Home
Java Tutorials
Book Reviews
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Java Network
Java Forums
Java Blog




Most Visited Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews
Top Rated Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews


Statistics
Registered Users: 4100
Java SE Tips: 614
Java ME Tips: 202
Java EE Tips: 183
Other API Tips: 779
Java Applications: 298
Java Libraries: 209
Java Games: 16
Book Reviews:
 
 
 
How to modify horizontal alignment of text field at runtime E-mail
User Rating: / 20
PoorBest 

This Java tip illustrates a method of modifying horizontal alignment of text field at runtime. Developer may use this code to design the interface at runtime. Using this code develoeper may also change the interface according to an input from user.


Image

import javax.swing.*;
import java.awt.event.*;

public class JTextFieldExample {

  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.LEFTtf.setHorizontalAlignment(JTextField.RIGHT);
    if (old == JTextField.RIGHTtf.setHorizontalAlignment(JTextField.CENTER);
    if (old == JTextField.CENTERtf.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(27575);
    frame.setVisible(true);
    tf.requestFocus();
  }
}

 Related Tips

 
< Prev   Next >

Page 1 of 0 ( 0 comments )

You can share your information about this topic using the form below!

Please do not post your questions with this form! Thanks.


Name (required)


E-Mail (required)

Your email will not be displayed on the site - only to our administrator
Homepage(optional)



Comment Enable HTML code : Yes No



 
       
         
     
 
 
 
   
 
 
java bottom left
java bottom middle
java bottom right
RSS 0.91 FeedRSS 1.0 FeedRSS 2.0 FeedATOM FeedOPML Feed

Home - About Us - Privacy Policy
Copyright 2005 - 2008 www.java-tips.org
Java is a trademark of Sun Microsystems, Inc.