It seems me to be so tired and silly now :) I want to fire button pressing event when certain condition is met. Here's my code:
#FXML protected void play(ActionEvent event) throws Exception {
if(menu.getText().equals("The certain text")) {
play.fireEvent(event); //Trying to cancel event
}
}
And when I press the button with fx:id "play", the app hangs. What am I doing wrong? Big thanks!
It is entering to the infinite loop. You are firing the event again in the event handler. However check the output log, there should be some exception thrown like "StackOverflowError", since the resources are not infinite :). So do not fire the event in the handler which can handle it again.
If you want to cancel the event, use event.consume() it will cause the action to stop.
Related
I'm currently working on a GWT project. I have a common block shared between multiple pages. I have some action buttons on that common block and the pages have a handler for the event launched on the click of those action buttons.
The problem I'm facing is that when I click on one of those action buttons on Page A, the handler from Page B previsouly registered would be called too.
So the solution I thought of was to remove the handler from a page when we leave it so there would be only one page at once with a registered handler to the same action button event.
First, I register to the action button click events and save the HandlerRegistration object returned from the addHandler method:
HandlerRegistration actionButtonClickEventHandlerRegistration=eventBus.addHandler(CommonBlockActionButtonClickedEvent.TYPE, someHandler);
And then, on page change event, I call removeHandler from the previously saved HandlerRegistration object
eventBus.addHandler(PageChangeEvent.TYPE, new PageChangeEventHandler() {
#Override
public void onMainPageChange(PageChangeEvent event) {
actionButtonClickEventHandlerRegistration.removeHandler();
}
});
So I do that on every pages, except that when I lauch my app and go to two of those pages, I get this error:
Caused by: java.lang.AssertionError: redundant remove call
Do you guys have any idea of why I'm getting this error or another way to solve my issue ?
Thanks a lot !
I would set the handler to null after removing it and I would check if it is actually null before removing it.
Like this:
eventBus.addHandler(PageChangeEvent.TYPE, new PageChangeEventHandler() {
#Override
public void onMainPageChange(PageChangeEvent event) {
if(actionButtonClickEventHandlerRegistration != null ) {
actionButtonClickEventHandlerRegistration.removeHandler();
actionButtonClickEventHandlerRegistration = null;
}
}
});
Nevertheless you seem to remove the handler at least twice and should check your program logic for that.
A good approach to do that is to set a breakpoint in the debugger (of your browser) on the line removing the handler. If you look at the call stack for every call to it, you should be able to spot the duplicate call and fix it.
when i click on a button the click event handler executes a code . If by mistake(if browser hangs) i click on the button twice the code gets executed twice.i dont want that to happen.
Any suggestions to stop that?
i suppose i should use a schedular or timer but i am not sure
below is the code:
public void onSendButtonClicked() {
disableButtons();
eventBus.fireEvent(new SendEmcsDeclarationEvent(getDeclaration(), getMsgType()));
}
You can - as Abdullah mentioned - disable/enable every widget in GWT with
widget.setEnable(false)
and
widget.setEnable(true).
If you want to lock the whole screen, create a modal popup, show it, after the button is pressed and hide it, after the code has finished.
public void onSendButtonClicked() {
myProgessBar.show();
eventBus.fireEvent(new SendEmcsDeclarationEvent(getDeclaration(), getMsgType()));
myProgressBar.hide();
}
If you are using a async call, you have to hide the progessbar in the callbacks. In this case the finally command might be executed before the callback is executed. In your case it might be a good idea to create a ShowProgressBarEvent and HideProgressbarEvent, so that you can use the progressbar in your whole application.
If your are using a widget library f.e.: GXT, you will find a ProgressBar ready to use.
Hope that helps.
The best way I can think of is to enable/disable the button itself so as to make sure that the code in handler is not called again until before the previous call finishes up.
public void onSendButtonClicked()
{
try
{
disableButtons();
eventBus.fireEvent(new SendEmcsDeclarationEvent(getDeclaration(), getMsgType()));
}
catch (Exception ex)
{
throw ex;
}
finally
{
enableButtons();
}
}
When I create a button, I always also add an animating gif (ajaxloader).
When the button is clicked I make the button invisble, the ajaxloader visible.
When the action is done, I make the ajaxloader invisible, and the button visible.
This way the user has some visual feedback that something is happening (what you don't get when disabling the button), and not the entire application gets blocked (as a modal does) which is one of the plus points using ajax.
I want to show a modal window (instead the confirm) when a user close the browser. The GWT documention remarks that (during the handler operation) no user UI may be displayed during the shutdown. Exists any way cancel the operation, from the client not from the user?
Thanks in advance,
Oscar.
You can't cancel the close event. The best you can do is to let the user press cancel. I think the reason for this is to prevent "bad" javascript from not letting you close your browser.
This displays a dialog with a cancel option.
Window.addWindowCloseListener(new WindowCloseListener()
{
public String onWindowClosing()
{
return "You are about to exit from the application. Are you sure?";
}
public void onWindowClosed()
{
//cleanup code
}
});
I have a text widget where I want to make enter event to behave like a tab event. so I capture the Key press event and raise a tab native event.
However the tab behavior is not reflected in the application. The code for event handler is
public void onKeyPress(KeyPressEvent event) {
int keyCode = event.getNativeEvent().getKeyCode();
if (keyCode == KeyCodes.KEY_ENTER) {
NativeEvent nativeEvent =
Document.get().
createKeyPressEvent(false,false,false,false,KeyCodes.KEY_TAB );
DomEvent.fireNativeEvent(nativeEvent, this, this.getElement());
}
When I use the deprecated createKeyPressEvent with more argument, it fires the tab event but the behavior is not as per the tab key press, which is to move to next widget. The new code changes from the above code in createKeyPress event line
as follows
NativeEvent nativeEvent =
Document.get().
createKeyPressEvent(false,false,false,false,
KeyCodes.KEY_TAB ,KeyCodes.KEY_TAB);
Note that manually firing an event
does not generate the default action
associated with that event. For
example, manually firing a focus event
does not cause the element to receive
focus (you must use its focus method
for that), manually firing a submit
event does not submit a form (use the
submit method), manually firing a key
event does not cause that letter to
appear in a focused text input, and
manually firing a click event on a
link does not cause the link to be
activated, etc. In the case of UI
events, this is important for security
reasons, as it prevents scripts from
simulating user actions that interact
with the browser itself.
From http://www.howtocreate.co.uk/tutorials/javascript/domevents.
I am using Syncfusion's TreeViewAdv control. I attach a KeypUp event handler to it and it worked as expected. The event is fired whenever I released a key. However, once I wrap it in a ScrollViewer, the event is not fired. I have to move the event handler up to the ScrollViewer itself to catch the event. I aslo checked the PreviewKeyUp event, at the ScrollViewer it is fired and the Handled is false. But the PreviewKeyUp for TreeViewAdv is not fired at all.
I try to replace the ScrollViewer with a StackPanel and everything is fine. So it seems that the ScrollViewer stops event propagation to its content. How can I get the event handled within the ScrollViewer?
I could see that this is happening only when you select item through mouse and then trying to catch KeyUp and KeyDown events. These events are firing properly when you navigate items through keyboard only such as using Tab Key.
This is known issue in TreeViewAdv control and Syncfusion fixed this. They promised like fix will be included in their forth coming release Vol1 2010 which is scheduled on end of Jan 2009.
As a workaround, you can use this code snippet to receive the event notification.
<Syncfusion:TreeViewAdv.Resources>
<Style TargetType="{x:Type syncfusion:TreeViewItemAdv}">
<EventSetter Event="MouseLeftButtonUp"
Handler="TreeViewItemAdv1_MouseLeftButtonUp"/>
</Style>
</Syncfusion:TreeViewAdv.Resources>
Event handler in C#
private void TreeViewItemAdv1_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
TreeViewItemAdv item= (sender as TreeViewItemAdv);
If(item != null)
{
item.Focus();
}
}
Thanks,
Madhan