File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes Multiple listeners Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Multiple listeners" Watch "Multiple listeners" New topic
Author

Multiple listeners

Mike Lipay
Ranch Hand

Joined: Sep 11, 2007
Posts: 171
Hope this is not too basic a question, but I don't work with Java enough for things to stick in my ancient mind.

I am creating a window with multiple text fields, unfortunately my training only included single field types (what can you say, it was really basic training ). What is the best way to handle multiple input, a single listener that identifies the field, or one listener for each field? Since my training didn't cover either scenario, examples of each / either would be appreciated.

Thanks,

Mike
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42927
    
  68
For input field I generally use one listener for each separate field. I could see using just one listener for all fields if the actions to be taken are either very simple (just a few lines of code), or if the actions are very similar to one another, so that otherwise there'd be code duplication.
Mike Lipay
Ranch Hand

Joined: Sep 11, 2007
Posts: 171
Ulf Dittmer wrote:For input field I generally use one listener for each separate field. I could see using just one listener for all fields if the actions to be taken are either very simple (just a few lines of code), or if the actions are very similar to one another, so that otherwise there'd be code duplication.


How do you code that? Again, my training only showed how to code a single listener for a single field. I'm not sure how to code multiple listeners, what changes?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42927
    
  68
Nothing changes. But I'm not sure what you're asking - if you know how to add one listener to one field, then it should be easy to add N listeners to N fields, no? What have you tried?
Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Posts: 679
http://www.coderanch.com/t/454492/Swing-AWT-SWT-JFace/java/put-all-my-action-listeners

did you see this thread from the other day, in it you will get an idea of what is involved with coding one listener for several text objects.

The following website teaches the strategy I use of one listener for multiple objects in its section on developing GUIs

http://chortle.ccsu.edu/CS151/cs151java.html
Mike Lipay
Ranch Hand

Joined: Sep 11, 2007
Posts: 171
Fred Hamilton wrote:http://www.coderanch.com/t/454492/Swing-AWT-SWT-JFace/java/put-all-my-action-listeners

did you see this thread from the other day, in it you will get an idea of what is involved with coding one listener for several text objects.

The following website teaches the strategy I use of one listener for multiple objects in its section on developing GUIs

http://chortle.ccsu.edu/CS151/cs151java.html


Thanks! That work well, almost perfect.

The only problem is that the text only comes in when the user hit "enter", is there a way to handle this if they tab from one field to the next?
Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Posts: 679
Mike Lipay wrote:
Fred Hamilton wrote:http://www.coderanch.com/t/454492/Swing-AWT-SWT-JFace/java/put-all-my-action-listeners

did you see this thread from the other day, in it you will get an idea of what is involved with coding one listener for several text objects.

The following website teaches the strategy I use of one listener for multiple objects in its section on developing GUIs

http://chortle.ccsu.edu/CS151/cs151java.html


Thanks! That work well, almost perfect.

The only problem is that the text only comes in when the user hit "enter", is there a way to handle this if they tab from one field to the next?


I have never looked into that, but this is how I would go about it. Someone else might be able to provide a specific answer.

there are various types of listeners you can register with a text field, InputMethodListener is one, it might have an answer. each listener triggers an event, and so different kinds of events are triggered so you need to look through the API docs for these listeners, and especially the corresponding events, to find what you are looking for.

in addition to the Sun Java Tutorial and API docs, you might find an answer in chapter 6 and 7 at the following site. It's a little dated, but the explanations of the concepts are good, and most of the material is still relevant.

http://www.faqs.org/docs/javap/

p.s. to find out all the different types of listeners a JTextField supports, go to the API doc for JTextField, and look at the various addWhateverListener methods that are inherited from JTextComponent, and maybe some of the other "parent" classes such as Component and JComponent. JText field may even have it's own addListener methods
Mike Lipay
Ranch Hand

Joined: Sep 11, 2007
Posts: 171
Many, many thanks to all. I got it working the way I wanted!

I have been looking at two of the books in the bunkhouse, any suggestions for topic like this?
Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Posts: 679
so which Listener was it that dealt with tabbing away from a JTextField?
Mike Lipay
Ranch Hand

Joined: Sep 11, 2007
Posts: 171
Actually, I went with a different design. The app will require clicking on "Save, Cancel, Clear" buttons, and the ActionListener will pull all of the information and present a warning if something is not filled in.
Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Posts: 679
Mike Lipay wrote:Actually, I went with a different design. The app will require clicking on "Save, Cancel, Clear" buttons, and the ActionListener will pull all of the information and present a warning if something is not filled in.


cool, there are always different ways off accomplishing the same purposes. Sometimes you will see different ways of doing data entry within the same application. This has value in that it accomodates the different preferences of the end user. Some prefer to navigate fields by clicking with their mouse, some prefer to use <enter> key, some prefer to use <tab> keys.

I don't have a clear vision of your GUI, but for people who are really adept at using this kind of program, and they use it a lot, they like to maximize productivity by minimizing the physical motion, in which case extending their left pinky over to tap the tab key to move between fields, and the right pinky for the enter key to save an entire record, and for these super users this strategy may be more efficient and productive than frequently reaching for the mouse. Just a thought.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Multiple listeners