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

Dani AI

Generated

JList doesn't raise ActionListener/ItemListener events — selection changes come from ListSelectionListener and model changes can generate ListDataEvents. As pointed out, that distinction matters: filling or replacing the model can cause the selection model to change and fire selection events even though no user action occurred.

A robust pattern is to build the new list model off‑screen and swap it in one atomic step on the EDT. That reduces intermediate noise and keeps the UI responsive if the data set is large. Around the swap, temporarily mark the selection model as “adjusting” so listeners that ignore adjusting events won't react:

DefaultListModel<String> m = new DefaultListModel<>();
for (String s : source) m.addElement(s);

// on EDT:
SwingUtilities.invokeLater(() -> {
    ListSelectionModel sm = list.getSelectionModel();
    sm.setValueIsAdjusting(true);
    list.setModel(m);
    list.clearSelection(); // avoid an automatic first-item selection
    sm.setValueIsAdjusting(false);
});

Other valid approaches are the ones already mentioned by and : use a lightweight guard flag in the listener, or remove and re-add the ListSelectionListener while updating. Also, have the listener ignore intermediate adjustments by checking if (!evt.getValueIsAdjusting()) so only the final selection is handled.

Final checks: perform heavy loading off the EDT (SwingWorker.publish/process), ensure all model/GUI changes happen on the EDT, and explicitly clear or set the selection after a model swap if a default selection is undesirable. These steps make list updates deterministic and stop spurious selection-handling during population.

Recommended Answers

All 7 Replies

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.

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

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.

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

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

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

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.