Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Text selection

Posted on 2005-05-05
4
Medium Priority
?
216 Views
Last Modified: 2010-03-31
Let's say I have a series of JTextFields:

textField1, textField2, textField3

When I tab through them, I would like for the current contents to be auto-selected (highlighted) so that if I start to type, it will replace the contents.  What is the easiest method to go about that?

Thanks,
David
0
Comment
Question by:lomidien
  • 3
4 Comments
 
LVL 92

Accepted Solution

by:
objects earned 2000 total points
ID: 13941251
Use a FocusListener on each text field that selects all on focusGained()
0
 
LVL 92

Expert Comment

by:objects
ID: 13941265
public class SelectAll implements FocusListener
{
   public void focusGained(FocusEvent event)
   {
      JTextField tf = (JTextField) event.getSource();
      tf.select(0, tf.getText().length());
   }

   public void focusLost(FocusEvent event)
   {
   }

}
0
 
LVL 92

Expert Comment

by:objects
ID: 13941269
>     tf.select(0, tf.getText().length());

that can be replaced with

tf.selectAll();

:)
0
 

Author Comment

by:lomidien
ID: 13941270
completely overlooked .selectAll() even though I had already consulted the api.  DOH  I was going through the trouble of creating a DefaultHighlighter and messing with that.

Thanks,
David
0

Featured Post

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses
Course of the Month10 days, 7 hours left to enroll

571 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question