• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Help Needed on Which Path to Pursue

 
Greenhorn
Posts: 80
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I am not sure which way to go in terms of researching how to solve my little issue. I have a few JTextFields where I want to take different action depending on which they click on (or move to with the keyboard). Whichever they choose will determine which options are available to the user.

I know I could duplicate code and use both a KeyListener and a MouseListener, but I think there must be a better way.

Thanks for the help

BD

Edit: I'd also like to know which JTextField is the one they selected. Would this be handled by the setActionCommand() or is there some way to test for focus?

Edit #2: I am reading about FocusListeners. Is it better to have the class implement FocusListener or create a new instance for each JTextField I want to watch?

Edit #3: Let me apologize for wasting your time. I was too quick to post this question, and have subsequently found my own answer. I'll try to resist the urge in the future until I am sure I can't find my own answer. :-)
 
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you tell us what solution you found, you could potentially help someone else with the same issue in the process. That's kind of how these forum things work.
 
Bd Howard
Greenhorn
Posts: 80
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Gregg,

Since I am not the brightest bulb in the lamp, I often need things spelled out to me, so thanks for the push. :-)

While the solution I am posting here works for me, I in no way am putting it forth as best practice. In fact, I hope someone will show me how I can do this better.

I have some JTextFields for user input, and if they input something in one field, I needed to preclude them from using one of the other JTextFields. I thought about the situation of the user entering data into one, and realizing they were using the wrong field. So I needed a way to "reset" the options back to a baseline. I knew this involved listeners of some sort, but I was not sure which to use, which led to my initial question.

I did not want to duplicate code using a MouseListener & KeyListener for each field, so I looked into FocusListeners. I set each JTextField to its own FocusListener, each with the unique code for its "reset" situation. I only used the override focusGained since losing focus was not important in this context. I thought about having my class implement FocusListener, but I needed more granularity in terms of each JTextField's special needs, so I used separate instances of FocusListener.

Now I am moving on to the next roadblock which is finding the power switch on my Mini. :-)

BD
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From the description, I'd say using a DocumentListener is the right way to go. You can 'reset' the state when the user deletes all text in the particular text field.
 
Bd Howard
Greenhorn
Posts: 80
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Darryl,

Thanks for that tip. I have never used nor heard of a DocumentListener. That list of classes and interfaces in Javadocs is so long, that I wonder if I'll ever get to use even half of them. I guess with each tip I get from JavaRanch, I am one class closer. :-)

I will look into that this evening.

BD
 
His brain is the size of a cherry pit! About the size of this ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic