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 Need to handle 2 event handlers in one class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Need to handle 2 event handlers in one class" Watch "Need to handle 2 event handlers in one class" New topic
Author

Need to handle 2 event handlers in one class

Colm Dickson
Ranch Hand

Joined: Apr 04, 2009
Posts: 87
Hi all,

I have a program using one Jlist where if I select the value using the mouse then the related values to the selected item are displayed in another Jlist on the form. This works fine but I noticed that I also need to handle thsi when the up-down keyboard keys are pressed.. I then implemented KeyListener as well as ListSelectionListener with all the required methods and I'm checking up KeyEvent up down etc..and in the keyPressed methods I want to place the same code as in the valueSelected method belonging to the ListSelectionListener interface but it does not have the required effect. before I post my code, can someone advise if I'm doing the right thing or can it be handled a better way?

Thanks,

Colm
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

Nothing prevents you from implementing different listeners in the same class. This should be fine.


[My Blog]
All roads lead to JavaRanch
Colm Dickson
Ranch Hand

Joined: Apr 04, 2009
Posts: 87
Thanks.

Yes I got this working.

In case anyone else wants to know, I added to separated event handlers to my JList. One for KeyListener and another for ListSelectionListener. I then had a class implementing both these interfaces with the code I wanted to fire in the keyPressed and keyReleased methods(implemented as part of the KeyListener interface) and the same code in the valueChanged method belonging to the ListSelectionListener Interface.

Thanks,


Colm
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 9990
    
    7

Colm Dickson wrote:
Yes I got this working.


Well done
And thank you for sharing your solution approach for future users.


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Need to handle 2 event handlers in one class
 
Similar Threads
How do I get a string from an item in a dynamic jlist?
Swing class and events
JList setListData problem
ListSelectionListener in JTable
getValueAt in JTable