accountTab.setChangeListener(new FieldChangeListener() {
public void fieldChanged(Field field, int context) {
if (field==TabBar.accountTab) {
Dialog.alert(" Account Tab is clicked ");
}
}
});
why this code is not working?
but some places it works. I am ceating the tab bar for 9800 torch with Horizontal field manager and "accountTab" in this code is the custom button field. Please Help me.........
If you are attaching a FieldChangeListener to one Field, you shouldn't really need to test whether or not it is the Field (unless you're doing something very custom). I would try debugging and see whether or not the fieldChanged call is getting hit, and if you want to use the test in there, make sure TabBar.accountTab is actually a reference to what you think it is.
Another possibility is that you haven't set the tab as FOCUSABLE, or possibly you have something further up the chain intercepting the clicks and not allowing it to proceed further.
The information provided is too vague to know the exact problem. However, there's a good chance it has to do with the fact that you're trying to open a dialog in response to an event and you might not currently be holding the event lock. Use Application#invokeLater() to open the dialog. You'll need to implement Runnable and make the Dialog.alert call from the run method.
does the callback fire? have you attached a debug breakpoint to the conditional to see if it actually calls?
if so, then #Fostah's answer above is a pretty good place to start, you may not be in the event dispatch thread when this callback fires...
Related
I have a JS component (SigPlot) that I need to read click values from. I have instantiated SigPlot inside of a VerticalLayout, where I also instantiate a DIV to pass to the SigPlot constructor. I am not sure if this is a valid way, but it works.
Now I need to read CLICKS but I am having troubles finding correct way to do this. Can someone pass some words of wisdom?
In my constructor for my VIEW, it use addAttachListener to start my JS code using.
div.addAttachListener(e->{
e.getUI().getPage().executeJs("myInit($0)",div);
});
How can I register a click listener to this?
Regards
As long as it's just a regular DOM event listener, then something like this should work:
div.getElement().addEventListener("click", event -> Notification.show("Clicked"));
If you need to do something on a more granular level, then your might want to expose callbacks as #ClientCallable and then use executeJs to run some short JavaScript snippets to set up listeners that delegate to those methods.
I am following this udemy course(https://www.udemy.com/learn-flutter-dart-to-build-ios-android-apps) on flutter, and using scoped_model for state management.
I want to edit a product from list of products. For that I set the product id as a flag - selectedProductId in scoped model. When submitting, I want to navigate away and set this selectedProductId to null.
//method to edit product
editProduct(productId, newValues);
Navigator.pushReplacementNamed(context, '/productsPage').then((_){
debugger();
setSelectedProductId(null);
}).catchError((err){print(err);});
The debugger is never hit. and there is no error either.
I tried using async await and still no use.
editProduct(editableProductId, _formData);
await Navigator.pushReplacementNamed(context, '/productsPage').catchError((err){print(err);});
debugger();
setSelectedProductId(null);
the page is navigated to the given route, but the line after, is never executed. I even tried using pushNamed instead of pushReplacementNamed.
I know that pushReplacementNamed returns a future, so .then() must be called, but doesn't seem to happen, am I missing something? Are there any other factors or situations I should know?
You have got the concept wrong. Suppose you have Activity A and Activity B. From Activity A you navigate from A to B. Now the then from A is called only if Navigator.pop(context) is called from B. If pop is not called from B then the then callback is never called.
In your case you are calling Navigator.pushReplacementNamed from A, so what you are doing is REPLACING A with B. So, even if you pop from B then Activity A is not available anymore, so the then callback will never be called.
You can refer to this section in the flutter cookbook for further info.
As for executing the code in then callback you should move it to dispose method of the Widget, you will see it getting called but I don't see the point in doing this because the Widget is getting destroyed and no point in setting its instance property as null.
Create a Custom Future Object might solve the Problem
Navigator.pushReplacementNamed(context, '/product');
Future.delayed(Duration(milliseconds: 700)).then((_)=>resetSelectedProduct(null));
Just simply add the below code:
Navigator.of(context).pushNamedAndRemoveUntil('/productsPage', (route) => false);
In the bad old days in my codebase we relied quite heavily on event requeuing, which I suspect worked due to implementation details in ICEfaces or MyFaces rather than standard-specified behavior. One thing we used to do frequently was this kind of thing:
<ice:inputText value="#{bb.frequency}" valueChangeListener="#{bb.valueChanged}"/>
The goal is to arrange for retune to be called after setFrequency whenever the frequency changes.
Then we had some fairly disgusting code in the backing bean which would requeue the event. It usually looked something like this:
class BB {
// this happens first, thanks to UPDATE_MODEL_VALUES
public void setFrequency(Frequency f) {
this.model.f = f;
}
public void valueChanged(ValueChangeEvent event) {
if (event.getOldValue().equals(event.getNewValue())
return; // nothing changed, so leave
if (FacesContext.getCurrentInstance().getPhaseId() != INVOKE_APPLICATION) {
OurMagicEventUtils.requeueEvent(event, INVOKE_APPLICATION);
}
else {
// do the post-setter work here (the setter happened recently during
// UPDATE_MODEL_VALUES so we're up-to-date by here
this.model.retune();
}
}
}
This isn't a good way to live. I haven't found a reliable way to requeue events for later phases and it clearly isn't the kind of thing people do. I see two solutions:
Move the retune intelligence to the BB#setFrequency method.
I can't get away with this in many cases because I'm directly addressing a lower-level model class and I don't want to disturb its behavior for other clients.
Create a custom component and move the logic into the setFoo method there.
I don't love this because there are a lot of issues with Mojarra and custom components when embedded in other containers. It also seems like overkill for what I need to do—I literally just need to call retune after setting some properties.
Create backing beans for everything. Delegate most methods directly to the inner thing, but catch setFoo and perform the retune there. This is very similar to what we used to do, and it means a lot of boilerplate, wrappers, and glue code, so I don't love it.
In my mind I imagine something like this:
<ice:inputText value="#{bb.frequency}" afterChange=#{bb.retune}"/>
but that obviously doesn't work, nor would attaching an <f:actionListener> since that requires a class name but has no association to whatever you're currently doing, and besides it can only be set on UICommands which UIInputs are not.
What's the elegant/correct way to solve this dilemma?
As you're using JSF2 already, just use <f:ajax>.
<ice:inputText value="#{bb.frequency}">
<f:ajax listener="#{bb.retune}"/>
</ice:inputText>
with
public void retune(AjaxBehaviorEvent event) { // Note: the argument is optional.
// ...
}
This will be invoked during invoke action phase when the HTML DOM change event has occured.
I have a Textbox and on the LostFocus event of it, I have a function called SaveWeight() to save the value based on few conditions. I need to programatically trigger that function.
For example, I have a OnClearButClicked function in which the the value in the textbox is cleared. So after it is cleared, I need to call the SaveWeight function. But since it is a LostFocus event I need to manually set the focus of the textbox to Unfocused which might hopefully trigger the SaveWeight().
private void OnClearButClicked(object sender, RoutedEventArgs e) {
weightTBox.Text = "";
weightTBox.Focus(Windows.UI.Xaml.FocusState.Unfocused);
}
But the weightTBox.Focus(Windows.UI.Xaml.FocusState.Unfocused); throws an ArgumentException. So is my approach wrong? Can I set the FocusState for an element and then trigger the event. Or some other thing I'm missing?
Why not just call the method, passing in null as the arguments?
SaveWeight(null, null);
It seems to me, setting focus in the UI is a huge violation of the separation of concerns in your code base.
I am creating an action script library.I am calling some APIs which parses some xml and gets me the result. It dispatches an Event.COMPLETE when the parsing is done. I want to monitor whether this event is dispatched in some while loop like "while(eventnotdispatched)"
is it possible? I know the other way would be to addeventlistener. But please let me know if the other thing is possible.
Thanks
NO, it is not possible. Actionscript is single threaded. Thus while you are waiting in your while loop, that is the only thread running, and the process you are waiting for can never complete. This is why everything is done with events, so that's what you should use. If you need to update your display periodically while you are waiting for something to complete...again, use events. Create a Timer object which generates a TIMER event every so often, and use that to make your updates.
EDIT: Davr is right, you would not be able to use the while loop like this. You would need a timer.
Yes, it is possible to poll for it. BUT you will still need to create an event listener. It will work something like this:
private var loadCompleted = false;
private var timer:Timer= new Timer(1);
private function onInitCompleted(event:Event):void
{
timer.addEventListener(TimerEvent.TIMER, timerHandler);
timer.start();
}
private function loadCompleteEventHandler(event:Event):void
{
loadCompleted = true;
...
}
private function timerHandler()
{
if(!loadCompleted)
{
... // stop the timer or something.
timer.stop();
}
}
Please note, this is VERY BAD code. I would NEVER use it in production because Actionscript is a event driven language. There should be absolutely NO REASON for you to need to do this. Whatever you are trying to do could be accomplished using another method much simpler. Tell me what you are trying to accomplish with this and I will present a better solution.
Sorry for yelling, it's late and I am sleepy.
Doing that means forcing a synchronous model of execution on the underlying asynchronous model (that works with callbacks).
What are you trying to achieve exactly, and why not use a callback?
I agree with the statements about it probably being a bad idea and a while loop will certainly not work this way in ActionScript. However, there may be legitimate reasons for doing what you are attempting to do. Only you can prevent bad code. Instead of judging, I'll just get to an answer for your question.
First I'm going to make an assumption, that what you really want to do is monitor a property and for some reason the API for this object does not dispatch an event when this property changes. I'm making this assumption because if you have the event available, I assume you would just use the event.
So... you have an object weirdXmlObj with a property loaded that defaults to false but goes to true when the XML is loaded.
In this case with slight modifications the code posted by CookieOfFortune would in fact work. You wouldn't need the loadCompleteEventHandler function (which was never attached anyway) and in the timer handler you would simply check if( weirdXmlObj.loaded ) and then branch however you wanted to.
Ah but there may be a simpler way, depending on what you are doing.
If you have a display object handy. (i.e. something that makes sense, not just some random object.) You can attach your code to the stage's EnterFrame event instead of using a timer.
myDisplayObject.stage.addEventListner(Event.ENTER_FRAME,frameEnterHandler);
A couple of things to be aware of:
You don't really even need to go to the stage level, all display objects support the EnterFrame event, but it's a nice place to attach the event listener.
You really should keep whatever the function calls to a minimum. In particular the actual frameEnterHandler function should do nothing more than do the if( weirdXmlObj.loaded ) check.
You are attempting to circumvent event-driven programming, which is not a good idea. This is often the case when someone approaches from an older model and does not yet have a good frame of reference to appreciate the elegance of event-driven programming.
Events are your friends. They work very well. Your loadCompleteHandler is all that is required. Want to do something else in response? Add the call in that handler:
private function loadCompletedHandler(event:Event):void
{
waitingObject.fileWasLoadedSoGoDoThatThing();
}
There is no need to make it any more complicated than that. No need for a semaphore or a loop to check the semaphore. Unnecessary environmental semaphores can break the encapsulation that could shield you from unwanted side effects.