0
votes

In My Eclipse Project, I have a

Text custom_text = new Text(....);

Now I add a listener -

custom_text.addKeyListener(new KeyListener(){

@Override public void keyPressed(KeyEvent event) {

}

@Override public void keyReleased(KeyEvent event) { System.err.println("event "+event.getSource()));

} });

Anyhow, I am not getting the source name,despite I am getting the output as Text {}. Well I want to get the source name ie custom_text .

How to get the output in a listener as custom_text

2

2 Answers

0
votes

IMHO you can not the name of the variable, holding the reference to your textfield. It is also not really of any use to know the name of the variable, since you can have many referencing variables.

With .getSource() you get a full reference to the widget itself, so you can deal with it in any way.

0
votes

You can use event.widget to identify wich widget notify event.

But general approach is relaying with anonymous listener. because it can use more readable method name which is more fit to subject of controller.

Text nameField, emailField = ...

nameField.addListener(SWT.KeyUp, new Listener(){
    public void handleEvent(Event e){
        handleNameEdited();   
    } 
});

emailField.addListener(SWT.KeyUp, new Listener(){
   public void handleEvent(Event e){
       handleEmailEdited();
   }
});