What I'm trying to do is fire an enter key press event in GWT.
This is my keyhandler:
itemBox.addKeyDownHandler(new KeyDownHandler() {
public void onKeyDown(KeyDownEvent event) {
if(event.getNativeKeyCode == KeyCodes.KEY_ENTER) {
// do something
}
Then later I wanna fire an enter key press event but I can't seem to figure out how I do this. I wanna do something like KeyDownEvent.setNativeKeyCode(KEY_ENTER).
textBox.fireEvent(new KeyDownEvent(null));
Is it possible to set these parameters?
You can fire the event using DomEvent.fireNativeEvent, instead of textBox.fireEvent.
Here is a working example how to do this:
final TextBox tb = new TextBox();
tb.addKeyDownHandler(new KeyDownHandler() {
#Override
public void onKeyDown(KeyDownEvent event) {
if(event.getNativeKeyCode() == KeyCodes.KEY_ENTER) {
Window.alert("enter!");
}
}
});
Button b = new Button("keyevent");
b.addClickHandler(new ClickHandler() {
#Override
public void onClick(ClickEvent event) {
DomEvent.fireNativeEvent(Document.get().createKeyDownEvent(false, false, false, false, KeyCodes.KEY_ENTER), tb);
}
});
RootPanel.get().add(tb);
RootPanel.get().add(b);
Related
For a quite simple element
public static class ToolHtml extends Anchor {
private Long toolId;
public ToolHtml(ToolDetails toolDetails) {
this.toolId = toolDetails.getToolId();
this.setText(toolDetails.getName());
Style style = this.getElement().getStyle();
style.setColor("orange");
this.getElement().setId(DOM.createUniqueId());
}
public Long getToolId() {
return this.toolId;
}
}
I am trying to add some event handlers. I am interested in the DragStartEvent but not even the ClickEvent seems to work and I simply can't figure out why:
public void addOfferItem(ToolHtml toolHtml) {
Set<ToolHtml> toolHtmlSet = this.toolIdToToolHtml.get(toolHtml.getToolId());
if (toolHtmlSet == null) {
toolHtmlSet = new HashSet<>();
this.toolIdToToolHtml.put(toolHtml.getToolId(), toolHtmlSet);
}
toolHtml.getElement().setAttribute("contenteditable", "false");
toolHtml.getElement().setAttribute("draggable", "true");
toolHtml.addBitlessDomHandler(new ClickHandler() {
#Override
public void onClick(ClickEvent event) {
LOGGER.fine("Click");
LOGGER.fine("Click");
LOGGER.fine("Click");
}
}, ClickEvent.getType());
toolHtml.addDomHandler((DragStartEvent event) -> {
LOGGER.fine("HANDLER DRAG START");
},
DragStartEvent.getType());
toolHtml.addDomHandler((DragStartEvent event) -> {
LOGGER.fine("DOM DRAG START");
},
DragStartEvent.getType());
toolHtml.addBitlessDomHandler((DragStartEvent event) -> {
LOGGER.fine("DRAG START");
},
DragStartEvent.getType());
toolHtmlSet.add(toolHtml);
// Attach/append to DOM
this.getElement().appendChild(toolHtml.getElement());
}
Does anybody know what the issue is here?
You append an element, not a Widget to which your handlers have been attached. You need to add a widget to a widget for all the functionality to work.
NB: Instead of toolHtml.addBitlessDomHandler you need to use
toolHtml.addClickHandler
How to remove the ClickHandler Event in GWT? I added addClickHandler() Event for a button and i want to remove the ClickHandler Event.I tried HandlerRegistration Method But it failed to remove the handler ,Here is a snippet :
notification.addClickHandler(new ClickHandler() {
#Override
public void onClick(ClickEvent event) {
// TODO Auto-generated method stub
}
});
I want to remove the handler with notification !
Note:
Notification is the button instance that calls the handler!
Each add...Handler method returns the HandlerRegistration interface. This interface contains the removeHandler() method. If you want to remove handlers, simple store the returned interface in a variable and call removeHandler when you want to remove the handler.
HandlerRegistration handler;
handler = button.addClickHandler(new ClickHandler() {
#Override
public void onClick(ClickEvent event) {
// ...
}
});
handler.removeHandler();
This worked for me, I get the Handler registration when I bind the event,
closeIconHandlerRegistration = closeImg.addClickHandler( new ClickHandler()
{
#Override
public void onClick( ClickEvent event )
{
addCloseClickHanlder();
}
} );
After that When I need to remove the handler...
if ( this.getCloseButtonHandlerRegistration() != null )
{
this.getCloseButtonHandlerRegistration().removeHandler();
this.getCloseImg().addClickHandler( new ClickHandler()
{
#Override
public void onClick( ClickEvent event )
{
SaveCancelCommissionChangeEvent saveEvt = new SaveCancelCommissionChangeEvent();
saveEvt.setSave( false );
tabEventBus.fireEvent( saveEvt );
}
} );
}
I want to prevent that when the user hits ENTER to select a Suggestion in the SuggestBox, that this Key event is bubbling up.
I have this code in the wrapping Composite :
Event.addNativePreviewHandler(new Event.NativePreviewHandler() {
#Override
public void onPreviewNativeEvent(NativePreviewEvent event) {
if (event.getTypeInt() == Event.KEYEVENTS) {
int key = event.getNativeEvent().getKeyCode();
if (key == KeyCodes.KEY_ENTER) {
event.cancel();
}
}
}
});
But this handler is never called.
I can't tell why your method is not working. But I have an alternative approach. Add a key event listener for suggest box. If the enter key is pressed, cancel the propagation of the event.
suggestBox.addKeyPressHandler(new KeyPressHandler() {
#Override
public void onKeyPress(KeyPressEvent event) {
int key = event.getNativeEvent().getKeyCode();
if (key == KeyCodes.KEY_ENTER) {
event.stopPropagation();
}
}
});
This worked better for me:
suggestBox.addKeyPressHandler(new KeyPressHandler() {
#Override
public void onKeyPress(KeyPressEvent event) {
int key = event.getNativeEvent().getKeyCode();
if (key == KeyCodes.KEY_ENTER) {
event.getNativeEvent().preventDefault();
}
}
});
#Jen,
This code restricts the action of the 'enter' key-press in the textarea (if suggestBox associates with the text area), even when suggestion list is not shown.
While my suggestion list is showing, pressing the 'ENTER' key triggered the event-handler addSelectionHandler() where I want to stop the propagation (addKeyPressHandler event-handler should not be triggered)
I want to disable the anchor link event when it clicked one time. I used anchor.setenabled(false) but nothing happend. When I click the same button again the event e is true. I want false at that time.
public void onCellClick(GridPanel grid, int rowIndex, int colindex,EventObject e)
{
if(rowIndex==0 && colindex==2){
tomcatHandler = "Start";
anchorStart.setEnabled(false);
}else if(rowIndex==0 && colindex==3){
tomcatHandler = "Stop";
****anchorStop.setEnabled(false);
anchorStart.setEnabled(false);
anchorRestart.setEnabled(true);****
}else if(rowIndex==0 &&colindex==4){
tomcatHandler = "Restart";
anchorRestart.setEnabled(false);
}
AdminService.Util.getInstance().tomcat(tomcatHandler,new AsyncCallback<String>() {
#Override
public void onSuccess(String result) {
imageChangeEvent(result);
}
#Override
public void onFailure(Throwable caught) {
}
});}
Anchors in GWT have always had a problem with setEnabled() because HTML doesn't support such a property. A quick workaround is to create a new widget that subclasses GWT's Anchor, adding the following override:
#Override
public void onBrowserEvent(Event event) {
switch (DOM.eventGetType(event)) {
case Event.ONDBLCLICK:
case Event.ONFOCUS:
case Event.ONCLICK:
if (!isEnabled()) {
return;
}
break;
}
super.onBrowserEvent(event);
}
This disables the passing of the browser event to GWT's Anchor class (summarily disabling all related handlers) when the link is double clicked, focused or clicked and is in a disabled state.
Source
It doesn't seem to actually disable the anchor, but it does retain the status that has been set with anchor.setEnabled(), so just test that within your handler e.g.
myAnchor.addClickHandler(new ClickHandler() {
#Override
public void onClick(ClickEvent evt) {
// write to filter and then call reload
if (((Anchor) evt.getSource()).isEnabled()) {
//do stuff
}
}
});
To clarify what double submit is: When the user clicks on a submit button twice, the server will process the same POST data twice. To avoid this (apart from disabling the button after a single submit), most web frameworks like Struts provide a token mechanism. I am searching for the equivalent of this in GWT.
If you want to avoid submitting twice, how about:
boolean processing = false;
button.addClickHandler(new ClickHandler() {
#Override
public void onClick(ClickEvent event) {
if (!processing) {
processing = true;
button.setEnabled(false);
// makes an RPC call, does something you only want to do once.
processRequest(new AsyncCallback<String>() {
#Override
public void onSuccess(String result) {
// do stuff
processing = false;
button.setEnabled(true);
});
});
}
}
});
That's the gist of it.
This will be helpfull for you -
final Button btn = new Button("Open");
btn.addSelectionListener(new SelectionListener<ButtonEvent>() {
#Override
public void componentSelected(ButtonEvent ce) {
btn.setEnabled(false);
openMethod(name, new AsyncCallback<Void>() {
public void onFailure(Throwable caught) {
btn.setEnabled(true);
}
public void onSuccess(Void result) {
MessageBox.alert(info, "Opened Window", null);
btn.setEnabled(true);
window.hide();
}
});
}
});