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: 1403
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:How to Group text color
Next Topic:Check button and other controls do not show border when focused
Goto Forum:
  


Current Time: Sun Oct 22 22:47:23 GMT 2017

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

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