How to Set the Cursor to a Specific JTextField

Written by alexander gokhfeld
  • Share
  • Tweet
  • Share
  • Pin
  • Email
How to Set the Cursor to a Specific JTextField
JTextField class allows programmers control string data type in Java programs. (Jupiterimages/Photos.com/Getty Images)

The JTextField class of Java programming language allows users to enter and display one line of text, defined as the String data type. When the user presses the "Enter" key, the action listeners attached to the text fields notify your application about input or output characters, selection or cursor movement. The JTextField class determines the cursor position within a string by using focus method. You must define the JTextField focus to set the cursor to a specific JTextField.

Skill level:
Moderately Easy

Other People Are Reading

Instructions

  1. 1

    Click the "Start" button in Windows and select "Java2EE."

  2. 2

    Click the "Open" and select the Java program in which you want to set the cursor to a specific JTextField.

  3. 3

    Add the following code after "System.Windows.Forms" in your program:

    public static void main (String [] args)

    {

    v1 = new JTextField (10);

    v1.addActionListener (new ActionListener ()

    {

    public void actionPerformed

    (ActionEvent e)

    {

    v2.requestFocus ();

    }

    });

    v2 = new JTextField (20);

    v2.addActionListener (new ActionListener ()

    {

    public void actionPerformed

    (ActionEvent e)

    {

    v1.requestFocus ();

    }

    });

  4. 4

    Click "File," then "Save" to set the cursor to a specific JTextField in your Java program.

Don't Miss

Filter:
  • All types
  • Articles
  • Slideshows
  • Videos
Sort:
  • Most relevant
  • Most popular
  • Most recent

No articles available

No slideshows available

No videos available

By using the eHow.co.uk site, you consent to the use of cookies. For more information, please see our Cookie policy.