Take the 2-minute tour ×

If it is taken for granted that you need to modify programatically an element of the UI, like, say, a JComboBox's selected item without triggering any modification/notification event, then how to do it relatively cleanly?

Surely I can detach all the listeners then reattach them once I've modified what I want to modify but, typically, how is one supposed to do that? (once again, by considering that it is granted that that some element shall be modified and that it is considered granted that those modification should not trigger any callbacks).

P.S: I did do it by writing methods that do actually detach all the listeners, perform the modification, then re-attach all the listeners and it does work, but it seems a bit crappy to me.

share|improve this question
This comment to emphasize the fact that, for example, one reason for this requirement would be the need to perform a tiny modification to a gigantic codebase which one didn't write and where there ain't time to cleanly redesign everything. –  SyntaxT3rr0r Nov 30 '10 at 19:56
add comment

2 Answers

Usually, the problem are not all listeners, but one - our own one. A typical pattern for ignoring such events is following:

private boolean ignoreEvents;

public void initialize(...) {
   ignoreEvents = true;
   try {
      // set the combobox value
   finally {
      ignoreEvents = false;

private void processMyXyEvent(...) {
   if (ignoreEvents) {

   // listener code
share|improve this answer
+1 interesting... In a way I wished there was something like that already present in the default APIs, say a "setButDoNotTrigger(...)" or something like that. That said, I take it that in the example you posted it's better to make ignoreEvents volatile right? –  SyntaxT3rr0r Dec 1 '10 at 12:28
IMHO there is no need to make it volatile, because it only should be accessed in the EDT. –  Mot Dec 1 '10 at 19:19
add comment

I think the methods you implement are the closest you can get to a clean way of doing what you're looking for. And while I understand the requirement is taken for granted, it might be worth noting that by avoiding all the set listeners (specifically, ALL of them, not just the ones you were setting up yourself), you might end up driving the state of your application to something that is internally inconsistent.

I suppose what I'm saying is you should proceed with care :)

share|improve this answer
add comment

Your Answer


