Skip to main content



      Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » Correct way to use Listeners
Correct way to use Listeners [message #636886] Wed, 03 November 2010 04:55 Go to next message
Eclipse UserFriend
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 10:04 Go to previous message
Eclipse UserFriend
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 Jul 12 21:08:25 EDT 2025

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

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

Back to the top