Monday, November 21, 2011

Get X and Y Coordinates of JTextField

    package org.best.example;
    /*
            Get X and Y Coordinates of JTextField Example
            This java example shows how to get X and Y coordinates of JTextField
            using Java Swing JTextField class.
    */
    
    
    import java.awt.FlowLayout;
    
    import javax.swing.JApplet;
    import javax.swing.JLabel;
    import javax.swing.JTextField;
    
    /*
    <applet code="JTextFieldGetXYCoordinatesExample" width=200 height=200>
    </applet>
    */
    
    public class JTextFieldGetXYCoordinatesExample extends JApplet{
    
            public void init(){
                  
                    //set flow layout for the applet
                    this.getContentPane().setLayout(new FlowLayout());
    
                    //create new JTextField
                    JTextField field = new JTextField("JTextField X Y Coordinates Example",20);
                  
                    /*
                     * To get X coordinate of JTextField use,
                     * int getX()
                     * method.
                     *
                     * To get Y coordinate of JTextField use,
                     * int getY()
                     * method.
                     */
                  
                    int x = field.getX();
                    int y = field.getY();
    
    
            }
    }

No comments: