Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>In general, there is no mechanism to do this. However, I managed to get it working by subclassing <code>StyledText</code>. The new class is called <code>StyledTextWithListeners</code>. You can just rename it if you want ;) . However, you will have to use</p> <pre><code>styledText.addListenerByUser(int eventType, Listener listener); </code></pre> <p>to add you <code>Listener</code>s rather than</p> <pre><code>styledText.addListener(int eventTyle, Listener listener); </code></pre> <p>This is necessary to discriminate between <code>Listener</code>s added by you and those added by SWT upon creation.</p> <p>To remove all listeners <strong>added by the user (you)</strong> just call</p> <pre><code>styledText.removeAllListeners(); </code></pre> <p>Here is the code:</p> <pre><code>import java.util.HashMap; import org.eclipse.swt.SWT; import org.eclipse.swt.custom.StyledText; import org.eclipse.swt.layout.GridLayout; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Event; import org.eclipse.swt.widgets.Listener; import org.eclipse.swt.widgets.Shell; public class MiniExample { public static void main(String[] args) { Display display = Display.getDefault(); final Shell shell = new Shell(display); shell.setLayout(new GridLayout(1, false)); StyledTextWithListeners text = new StyledTextWithListeners(shell, SWT.BORDER); text.setText("TEXT"); text.addListenerByUser(SWT.Verify, new Listener() { @Override public void handleEvent(Event arg0) { } }); text.removeAllListeners(); shell.pack(); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } } public static class StyledTextWithListeners extends StyledText { HashMap&lt;Integer, Listener&gt; listeners; public StyledTextWithListeners(Composite parent, int style) { super(parent, style); } public void addListenerByUser(int eventType, Listener listener) { addListener(eventType, listener); System.out.println("Adding: " + listener.getClass().toString()); if(listeners == null) listeners = new HashMap&lt;Integer, Listener&gt;(); listeners.put(eventType, listener); } public void removeAllListeners() { for(Integer type : listeners.keySet()) { System.out.println("Removing: " + listeners.get(type).getClass().toString()); removeListener(type.intValue(), listeners.get(type)); } listeners = new HashMap&lt;Integer, Listener&gt;(); } } } </code></pre> <p>It basically saves all the <code>Listener</code>s you add in a <code>HashMap</code> and removes them when you call <code>removeAllListeners</code>.</p> <p><strong>Keep in mind</strong> that I didn't take care of the case where you add a second <code>Listener</code> with the same <code>eventType</code> to the <code>StyledText</code> as done before. In this case, the old <code>Listener</code> will be replaced in the HashMap, but not removed from the <code>StyledText</code>. If this case can occur in your scenario, just add some code yourself.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload