Is there an easy way to change the behavior of a Java/Swing control when it gets focus?

Question :

Is there an easy way to change the behavior of a Java/Swing control when it gets focus?,

Answer :

For most GUI’s I’ve used, when a control that contains text gets the focus, the entire contents of the control are selected. This means if you just start typing, you completely replace the former contents.

Example: You have spin control that is initialized with the value zero. You tab to it and type “1” The value in the control is now 1.

With Swing, this doesn’t happen. The text in the control is not selected and the carat appears at one end or another of the existing text. Continuing the above example:

Read More  Instrumenting Database Access

With a Swing JSpinner, when you tab to the spin control, the carat is at the left. You type “1” and the value in the control is now 10.

This drives me, (and my users) up a wall, and I’d like to change it. Even more important, I’d like to change it globally so the new behavior applies to JTextField, JPasswordField, JFormattedTextField, JTextArea, JComboBox, JSpinner, and so on. The only way I have found to do this to add a FocusAdapter to each control and override the focusGained() method to Do The Right Thing[tm].

Read More  Do you name controls on forms using the same convention as a private variable?

There’s gotta be an easier, and less fragile way. Please?

EDIT: One additional piece of information for this particular case. The form I am working with was generated using Idea’s form designer. That means I normally don’t actually write the code to create the components. It is possible to tell Idea that you want to create them yourself, but that’s a hassle I’d like to avoid.

Motto: All good programmers are basically lazy.

That’s the answer Is there an easy way to change the behavior of a Java/Swing control when it gets focus?, Hope this helps those looking for an answer. Then we suggest to do a search for the next question and find the answer only on our site.

Read More  Any restrictions on development in Vista

Disclaimer :

The answers provided above are only to be used to guide the learning process. The questions above are open-ended questions, meaning that many answers are not fixed as above. I hope this article can be useful, Thank you