Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » Correct way to use Listeners
Correct way to use Listeners [message #636886] Wed, 03 November 2010 08:55 Go to next message
Peter J. Kranz is currently offline Peter J. KranzFriend
Messages: 10
Registered: November 2010
Junior Member
Hello,

currently i'm refactoring some code at work, and i'm not sure if everything is correct. Also, i'm new to programming Eclipse RCP, but have experience with Java/Swing.

import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.widgets.Text;

public class TestClass implements ModifyListener {
	private Text textField;
	
	public TestClass() {
		// init fields
		textField.setText("foobar");
		
		textField.addModifyListener(this);
	}
	
	public void update() {
		textField.removeModifyListener(this);
		
// toggle visibilty of elements or change data
		if (textField.getText().equals("someString")) {
			textField.setText("bla");
		}
		
		textField.addModifyListener(this);
	}
	
	@Override
	public void modifyText(ModifyEvent e) {
		if (e.getSource() == textField) {
			//
		}
		
		// update all fields etc.
		update();
	}
	
}


i don't like removing the modifylistener and add it afterwards. Is there some better way?
Re: Correct way to use Listeners [message #636948 is a reply to message #636886] Wed, 03 November 2010 14:04 Go to previous message
David Wegener is currently offline David WegenerFriend
Messages: 1445
Registered: July 2009
Senior Member
Add a private boolean updateInProgress instance variable to the class and initialize it to false.

Add a check of the variable to the if statement in the update method.

Set the field to true just before calling setText on the text field.

Set the field back to false after the setText method.
Previous Topic:Drawing to a GC through an external native library
Next Topic:Use Menu with Combo?
Goto Forum:
  


Current Time: Sat Apr 20 04:01:36 GMT 2024

Powered by FUDForum. Page generated in 0.03130 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top