Vaadin 7: Detect Enter Key in a TextField
Vaadin 7: Detect Enter Key in a TextField
This can be easily achieved just by adding a shortcutlistener whenever the TextField gets the focus and remove it when it blurs. From an IOC perspective wrapping the TextField would most probably result in less rework than extending the TextField:For example:
TextField myTextField = new TextField("A text field");
myTextField.setImmediate(true);
OnEnterKeyHandler onEnterHandler=new OnEnterKeyHandler(){
@Override
public void onEnterKeyPressed() {
Notification.show("Voight Kampff Test",
Notification.Type.HUMANIZED_MESSAGE);
}
};
onEnterHandler.installOn(myTextField);
-------------------------------------------------------------------
And the code for OnEnterKeyHandler is:
import com.vaadin.event.FieldEvents;
import com.vaadin.event.ShortcutAction;
import com.vaadin.event.ShortcutListener;
import com.vaadin.ui.TextField;
public abstract class OnEnterKeyHandler {
final ShortcutListener enterShortCut = new ShortcutListener(
"EnterOnTextAreaShorcut", ShortcutAction.KeyCode.ENTER, null) {
@Override
public void handleAction(Object sender, Object target) {
onEnterKeyPressed();
}
};
public void installOn(final TextField component)
{
component.addFocusListener(
new FieldEvents.FocusListener() {
@Override
public void focus(FieldEvents.FocusEvent event
) {
component.addShortcutListener(enterShortCut);
}
}
);
component.addBlurListener(
new FieldEvents.BlurListener() {
@Override
public void blur(FieldEvents.BlurEvent event
) {
component.removeShortcutListener(enterShortCut);
}
}
);
}
public abstract void onEnterKeyPressed();
}
Enjoy at your own risk.
Thank you :)
ReplyDeleteYou are very welcome.
ReplyDeleteThank you guy! its 100% effective! Happy new Year.
ReplyDelete:) Happy new year.
DeleteHi, Its working great in textFields, but I have one problem.
ReplyDeleteIts not working properly on DateFields
any thoughts?
Thank you
In theory it would be possible to make it work in every 'Component'. Just by replacing the 'TextField' parameter on [installOn(final TextField component)] by [installOn(final Component component)]. I have used that approach and it does work, please, let me know if it does when DateFields are used. Thank you.
DeleteYou rock :) . Thanks dude.
ReplyDeleteThanks, here is my take on the solution with Java 8:
ReplyDeleteimport java.util.function.BiConsumer;
import com.vaadin.event.ShortcutAction;
import com.vaadin.event.ShortcutListener;
import com.vaadin.ui.AbstractTextField;
public abstract class AddHandler {
public static void onEnter(AbstractTextField component, BiConsumer doOnEnter){
ShortcutListener enterShortCut;
enterShortCut = new ShortcutListener("EnterOnTextAreaShorcut", ShortcutAction.KeyCode.ENTER, null) {
@Override
public void handleAction(Object sender, Object target) {
doOnEnter.accept(sender, target);
}
};
component.addFocusListener(event -> component.addShortcutListener(enterShortCut));
component.addBlurListener(event -> component.removeShortcutListener(enterShortCut));
}
}
-----------------------------------
Usage:
TextField textField = new TextField();
AddHandler.onEnter(textField, (Object sender, Object target)->{
System.out.println("Works");
});
Thank you :)
DeleteThank you for this. Work like a charm.
ReplyDelete:)
DeleteWorks like a charm, thank you!
ReplyDelete