1,105,427 Community Members

How to stop a JList from firing events when contents is updated?

Member Avatar
SeanC
Light Poster
44 posts since Jul 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Hi all, I'm currently trying to solve a problem with a JList. I've got an actionPerformed event applied to the JList (also tried the item updated event but it has the same effect), and whenever this event is fired, (i.e. the user makes a selection), something happens. Now, the problem is that when the list is being populated, it seems to fire the event too - thus creating problems for me.

Is there a way to stop the JList from firing the actionPerformed or itemStateChanged event when the contents is updated?

Thanks

P.S. Question has also been asked here: http://www.java-forums.org/awt-swing/41914-how-stop-jlist-firing-events-when-contents-updated.html#post195009

Member Avatar
Ezzaral
Posting Sage
7,431 posts since May 2007
Reputation Points: 2,714 [?]
Q&As Helped to Solve: 953 [?]
Skill Endorsements: 31 [?]
Moderator
Featured
 
0
 

Either populate the list before you add the listener or set a boolean flag initialized and check if (initialized) {... in your listener.

Edit: You may check to see if ListSelectionListener works better for your needs also. I wouldn't think that one would fire any event as the model is filled.

Member Avatar
SeanC
Light Poster
44 posts since Jul 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Thanks for your reply! Could you kindly reply on the initialize part, haven't understood completely?

Member Avatar
Ezzaral
Posting Sage
7,431 posts since May 2007
Reputation Points: 2,714 [?]
Q&As Helped to Solve: 953 [?]
Skill Endorsements: 31 [?]
Moderator
Featured
 
0
 

You can set a flag while you are altering the contents of the control, such as initialized=false; and then set it to true when you are finished. Your listener can simply ignore events while you are updating the contents by checking that flag.

Member Avatar
SeanC
Light Poster
44 posts since Jul 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Oh I see what you mean, thanks :) I'll try it out!

Member Avatar
mKorbel
Nearly a Posting Virtuoso
1,346 posts since Feb 2011
Reputation Points: 274 [?]
Q&As Helped to Solve: 277 [?]
Skill Endorsements: 14 [?]
 
0
 

but as Darryl said (Java Forum) remove and add just if needed, if you'll able to set boolean flag (Ezzeral sugestion), then you'll be able same way remove&add whole Listener, that's good concept and the safiest

Member Avatar
SeanC
Light Poster
44 posts since Jul 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

I see - thanks for your input. I'll try both techniques and see which one provides the best results. Thanks!

Member Avatar
Ezzaral
Posting Sage
7,431 posts since May 2007
Reputation Points: 2,714 [?]
Q&As Helped to Solve: 953 [?]
Skill Endorsements: 31 [?]
Moderator
Featured
 
0
 

Are you actually using the AWT List component? JList doesn't provide methods to add ActionListener or ItemListener.

JList just uses the ListSelectionListener which behaves a bit differently.

Question Answered as of 3 Years Ago by Ezzaral and mKorbel
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: