aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Help Needed on Which Path to Pursue Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Help Needed on Which Path to Pursue" Watch "Help Needed on Which Path to Pursue" New topic
Author

Help Needed on Which Path to Pursue

Bd Howard
Greenhorn
Ranch Hand

Joined: Mar 30, 2012
Posts: 80
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. :-)


I've got just enough Java knowledge to royally screw everything up. :-)
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15292
    
    6

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.


GenRocket - Experts at Building Test Data
Bd Howard
Greenhorn
Ranch Hand

Joined: Mar 30, 2012
Posts: 80
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
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4523
    
    5

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.


luck, db
There are no new questions, but there may be new answers.
Bd Howard
Greenhorn
Ranch Hand

Joined: Mar 30, 2012
Posts: 80
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Help Needed on Which Path to Pursue
 
Similar Threads
testing tools
transform ResultSet data into a DTO
Spring Live in Sourcebeat?
How to call Jasper Report
Servlet compiler error