Sunday, March 6, 2016

How to "Select All" or Highlight textfield or password field in Java

There are cases when you want to select all or highlight the characters inside a textfield or password field in your Java desktop app. Here's a sample code to do that:

int i = textfield.getText().length();
textfield.setSelectionStart(0);
textfield.setSelectionEnd(i);


Put this in the action or wherever you want it. Replace textfield variable to the name of your textfield. I'm not sure but this might work for JTextArea and JTextPane too.

0 comments:

Post a Comment