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.
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. :-)
I've got just enough Java knowledge to royally screw everything up. :-)
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. :-)
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.
There are no new questions, but there may be new answers.
Joined: Mar 30, 2012
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. :-)