Forums Register Login

Help Needed on Which Path to Pursue

+Pie Number of slices to send: Send
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. :-)
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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
This is my favorite show. And this is my favorite tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 799 times.
Similar Threads
Rate My GUI
Select JTextField value in a DefaultCellEditor
KeyListener not working for JLabel.
problem with JTextField
JTable with CellEditor peculiar focus problem
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 04:01:00.