Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to UnFocus a JTextField

When my application loads, which is made using netbeans, the first JTextField is automatically focused, and in this JTextField, I have written "Enter your Username", it will go away when the user clicks on this field, but when the application is loaded, this field is focused, means I can't see "Enter your Username", how to unfocus it on startup ?

like image 970
Ali Bassam Avatar asked May 27 '12 10:05

Ali Bassam


People also ask

How do you make a JTextField not editable?

In order to create a non editable JTextField , all you have to do is: Create a class that extends JFrame . Create a new JTextField . Use setEditable(false) that sets the specified boolean to indicate whether or not this textfield should be editable.

How do I limit the length of a JTextField?

We can restrict the number of characters that the user can enter into a JTextField can be achieved by using a PlainDocument class.

What method is used to read the text from a JTextField?

Notice the use of JTextField 's getText method to retrieve the text currently contained by the text field. The text returned by this method does not include a newline character for the Enter key that fired the action event.

What is the difference between JTextField and JTextArea?

The main difference between JTextField and JTextArea in Java is that a JTextField allows entering a single line of text in a GUI application while the JTextArea allows entering multiple lines of text in a GUI application.


1 Answers

textField.setFocusable(false);
textField.setFocusable(true);

If, and only if, textField had focus, the next component in TAB-order order will get focus automatically. The effect is the same as a TAB press.

(not tested in a GUI with just one focusable component :) )

like image 74
Mark Jeronimus Avatar answered Sep 29 '22 23:09

Mark Jeronimus