aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Three listners with a JTextFiled Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Three listners with a JTextFiled" Watch "Three listners with a JTextFiled" New topic
Author

Three listners with a JTextFiled

Damithri Warnakulasuriya
Greenhorn

Joined: Apr 18, 2008
Posts: 17
Hi all
I'm using a JTextField with three listeners as follows for three different purposes


nameTextField.addKeyListener( formatAction );
nameTextField.addFocusListener( formatAction );
nameTextField.addMouseListener(popupListener);

But the last one is working only if I comment the first two lines. Could anyone please suggest me a way to have three listeners work together.

Thanks
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10523
    
    9

Since you haven't provided any code it is difficult to say.
However you can try the following:
1)Looks like you are trying to trap what the user has typed and take some decisions accordingly.In that case you can use DocumentListener instead of KeyListener
2)Looks like you are trying to do some validation when the component gains/looses focus.In that case you can use the InputVerifier instead of the focus listener
3)Looks like you are using the MouseListener to display the popup menu. If you are using 1.5+ you can use the setComponentPopupMenu() method.

Please note I am just shooting in the dark here, as you haven't provided any code.


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Damithri Warnakulasuriya
Greenhorn

Joined: Apr 18, 2008
Posts: 17
Hi Maneesh
thank you very much for the early reply. I use popup menu to get copy past and cut commands. But when i write something and select some text to copy then i write click the mouse to get the popup menu to select copy command the earlier selected text is no longer highlighted. So it passes null to copy and doesn't do anything.
That is because when i press the mouse again focus listener takes place.
What i want is to keep the selected text till it calls my popup listener. I'm sorry I have no idea that which part of my code to send you as it is too long.
If you can tell me the part you want to give a solution I'll send you soon.
Thank you
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10523
    
    9

Drop the mouse and focus listeners. You dont need them.
Try with the setComponentPopup method I mentioned.
Damithri Warnakulasuriya
Greenhorn

Joined: Apr 18, 2008
Posts: 17
Hi.
The person who worked with this code earlier had added this focus listener to do some sort of validation. So it is needed to keep that listener as i do not have permission to modify the previous code.
So if i use the setComponentPopupMenu method, again i need the selected text. But because of the focus listener i'm unable to do it.
So i need a way to select text without removing the focus listener
thank you.
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10523
    
    9

Cant you switch to InputVerifier instead of the FocusListener?
Damithri Warnakulasuriya
Greenhorn

Joined: Apr 18, 2008
Posts: 17
You mean to switch in to InputMethodListener. No I'm unable to do that. Because the previous guy has implemented focus listener methods in another class and here also he has done it as follows
FormatAction formatAction = new FormatAction();
nameTextField.addFocusListener( formatAction );


NameFormatAction class is the place where he has implemented focus listener methods and if I changed it to another listener, classes that are using NameFormatAction also has to be changed and it 'll be a big issue.
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10523
    
    9

Ok then.
Here is the only thing I can think of.

Add a mouse listener. On mouseEntered you remove the focus listener and on mouseExited you add it again.

Anyone else got any better ideas?
Damithri Warnakulasuriya
Greenhorn

Joined: Apr 18, 2008
Posts: 17
Hi
With the permission I've changed the old code. Removed KeyListener and Focus Listener and added the DocumentListener as you said. Now it is working properly.
Thank you very much for the great great help.
Again Thanks a lot
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10523
    
    9

You are welcome
 
Don't get me started about those stupid light bulbs.
 
subject: Three listners with a JTextFiled