Reputation: 1427
I have a SWT Text
component with a Modify listener and when I write something on the Text, the listener code is executed, inside the listener I'm getting and printing the new text, so for example, if the original text was "initial text"
and when I type something, let's say "initial text2"
, the listener will print "initial text2"
, which is fine, but I also need the original text before the text was modified.
Is there a way to do this? I don't want to use keyPressed
listener because it will not handle when the user pastes the text using the mouse.
So far my code looks like this:
Text myText = new Text(parent, SWT.NONE);
myText.setText("initial text");
myText.addModifyListener(new ModifyListener() {
public void modifyText(ModifyEvent e) {
//Get here the original text
String content = myText.getText(); //This line return the modified string
}
});
Upvotes: 3
Views: 1995
Reputation: 36884
Listen for SWT.Verify
instead of SWT.Modify
:
Text text = new Text(shell, SWT.BORDER);
text.addListener(SWT.Verify, new Listener()
{
@Override
public void handleEvent(Event e)
{
// Get the source widget
Text source = (Text) e.widget;
// Get the text
final String oldS = source.getText();
final String newS = oldS.substring(0, e.start) + e.text + oldS.substring(e.end);
System.out.println(oldS + " -> " + newS);
}
});
This will print the text before and after the modification.
Upvotes: 7