tags:

views:

20

answers:

2

I have a JTextField with a Suggestion-Popup. A DocumentListener listen to all InsertUpdate-Events and check the Suggestion-List and open the Popup. If you click on a Suggestion-Entry in the Popup, the choosen Word will be set to the JTextField and the Popup closes. But this will fire a new InsertUpdate-Event and the Popup opens again.

I do not want to use a boolean flag or remove and add the Listener so often. Is there another way to prevent a DocumentEvent cycle? Or is there a Way to set the Text silently? I tryed:

 this.getTextComponent().getDocument().insertString()
 this.getTextComponent().setText()
+1  A: 

There is no way to insert the text without firing an event nor is there a second API which fires different events for changes by the user and changes by the code (as in Qt, for example). You have to use a boolean guard or disable the listener in another way.

Btw, SWT suffers from the same bad design.

Aaron Digulla
I don't see why you wouldn't want to use the boolean guard.
Erick Robertson
The boolean guard is bad design; what you really want is to be able to distinguish between user generated events and events that happen because the model is updating the UI after some change. If you can't, then you need boolean guards, you need to compare field values to see if they really changed (so you can break event cycles), etc. It also prevents multi-threaded UIs (todays UIs all run in a single thread). It's a limitation we got used to but it's still a design flaw.
Aaron Digulla
+1  A: 

1) remove the listener
2) insert the popup text
3) add the listener

camickr