• Post Reply Bookmark Topic Watch Topic
  • New Topic

multiple textfields using same FocusGained event  RSS feed

 
Dan Nicholson
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
I have a bunch of textboxes in a panel and I wanted to perform the same code when the user clicks into a textbox (select all the text in it). Right now I've started adding a focusgained listener to each textbox like this:



Is there a way I can have one focusgained listener that all the textfields can point to but have it pick the right textfield in the textFieldx.selectAll();?
How would I accomplish this?
Thanks
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A better way to do it would be
1) Create a custom class which extends JTextField
2) Have it implement FocusListener
3) In focusGained() select all the text
4) In your application use instances of this custom class.

This question will be better suited on the GUI forums. Moving....
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another way is to add the same FocusListener (extending FocusAdapter) to all the JTextFields, get a reference to the source of the FocusEvent, cast it to JTextField and selectAll().

Oh, and if you must use a visual designer, it's important to change your variable and method names to something meaningful. Appropriate imports would get rid of the ugly fully qualified class names.
 
Rob Camick
Ranch Hand
Posts: 2756
12
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could use the KeyboardFocusManager to add this functionality to all text fields in our application.

See: http://tips4java.wordpress.com/2009/08/30/global-event-listeners/
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dan Nicholson wrote:Is there a way I can have one focusgained listener that all the textfields can point to but have it pick the right textfield in the textFieldx.selectAll();?
How would I accomplish this?

I should point out at the start that I'm no GUI expert, but what you're asking sounds to me like you're mixing up "what" and "how".

It seems to me that you have multiple fields that you want to behave the same way, but I'm not sure that that necessarily means that they need to have the same listener.
What about different listeners that execute the same code (or method), but just apply it to different field instances? That sounds suspiciously like inheritance (or a specific type of listener) to me.

However, as I say, I'm no GUI expert, so I could be totally out to lunch.

What I would advise though: Work out what you want BEFORE you start deciding how you're going to implement it.

HIH

Winston
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!