How to Set the Cursor to a Specific JTextField
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.
Instructions
-
-
1
Click the "Start" button in Windows and select "Java2EE."
-
2
Click the "Open" and select the Java program in which you want to set the cursor to a specific JTextField.
-
-
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
Click "File," then "Save" to set the cursor to a specific JTextField in your Java program.
-
1
References
- Photo Credit Jupiterimages/Photos.com/Getty Images