Programmatically remove a listener added using FXML? - listener

I am adding a listener using FXML:
<RadioButton onAction="#onSelectionChanged" />
Now I need to temporarily disable this listener programmatically.
Now I could set some boolean variable "listenerDisabled" and check this variable in the listener, but I want a way to disable a listener without changing it - so I want to remove it.
The problem here is: How do I reference the listener in my code, so I can use the following?
RadioButton.selectedProperty().removeListener(<what to place here?>)
Thanks for any hint!

Add an fx:id specification to your fxml:
<RadioButton fx:id="myRadio" onAction="#onSelectionChanged" />
In the corresponding controller for the fxml, use the #FXML notation to have the FXMLLoader inject a reference to the radio button into your controller:
#FXML RadioButton myRadio;
To get a reference to the listener, invoke getOnAction:
EventHandler<ActionEvent> myRadioActionEvent = myRadio.getOnAction();
To remove the listener, use setOnAction:
myRadio.setOnAction(null);
To add the listener back again, use setOnAction again:
myRadio.setOnAction(myRadioActionEvent);
I didn't try any of the above, but I don't see why it would not work.

Related

polymer - remove tap listener from light dom

i've just started web programming and have a problem with removing a tap listener added with polymer.
My component 'my-button' has a tap listener:
listeners: {
tap: '_onTap'
}
my-buttons can be added to a my-button-group.
<my-button-group>
<my-button></my-button>
<my-button></my-button>
...
</my-button-group>
if a button was added to a my-button-group i want to remove the tap listeners of all buttons in the group.
i've tried to iterate over the groups children, but i can't find the listener in the childrens attributes/properties.
thank you
I assume you are iterating over the children with Polymer.dom(group).functions then call node.unlisten() to remove listener (see https://www.polymer-project.org/1.0/docs/api/Polymer.Base and go to the methods and the api. You can use this.unlisten() if you are working within an element, but any reference to the element will work. Remember that the nodes you are working with are "distrubuted" (ie they come from the content) so you will need to use Polymer.dom(mygroup).getDistributedNodes();

FLEX : avoid memory leaks

If in a component I have basic Button which can propagate MouseEvent.CLICK like this :
<s:Button id="btn" click="someFunction(event)" />
Should I manually remove this event if I remove my component with parent.removeElement(myButton) function or is it automatically removed ?
Thanks for clarification
Of course it will automatically removed event once you remove component(Button). There is no need to remove event manually then.
For Timer:
Once you set timer = null it should be ready for garbage collection. In smaller projects in most cases it makes no difference. In bigger one it probably will make difference. But, Personally, In my code, I would still remove the listener.

Does ActionScript 3 have some sort of event delegation system?

I have a container with many images. Instead of adding a listener for click and other mouse events on each of the images, I would like to only listen for those events on the parent of the image.
Is that possible?
container.addEventListener(MouseEvent.CLICK, clickHandler);
private function clickHandler(e:MouseEvent):void {
trace(e.currentTarget); // references container
trace(e.target); //references container's child or container itself depending on what has been clicked
}
If i am understanding your question correctly, that is totally possible. So assuming you have something like:
parent.addChild(new Child());
parent.addChild(new Child());
parent.addChild(new Child());
parent.addChild(new Child());
Then you should be able to bind the event listener to the parent thusly:
parent.addEventListener(MouseEvent.CLICK, handleClick);
and then your handler should look something like
private function handleClick(e:MouseEvent) {
// cast the target of the event as the correct class
var clickedChild:Child = Child(e.target);
// Do whatever you want to do.
}
You can also combine this with the useCapture argument of addEventListener to attach the event on the capturing side of the event rather than the bubbling side. And also use the .stopPropagation() method on the Event to stop any other event handlers from firing as well...
But its difficult to say if you need to use those without knowing more about what you are trying to do. But hopefully that will give you a push in the right direction.

In flex4 & as3 how do you change the button label dynamically?

Is there any way to change Spark button label dynamically? When i click on it, i want the label to change. I bind the String to label and gives value for the first time, but even flashBuilder shows me that Data binding will not be able to detect assignments.
Here is my button:
<s:Button name="button" label="{butt}" x="5" y="3" useHandCursor="true"
click="start()" buttonMode="true" cornerRadius="5"
skinClass="skins.CustomButtom"/>
And here is assigment:
public var butt:String = "Start";
Update
Both answers work.
Make the variable Bindable like this:
[Bindable]
public var butt:String = "Start";
It is not advisable to have buttons with changing labels.
Even if you must, it is preferable to change the label property directly instead of introducing a binding because Flash Player needs to instantiate extra listeners for bound variables.
In this case, a binding is required only if you are going to be changing the label frequently.
Without the bindable, you might have noticed that Flash will assign the value "Start" to the label of the button (generally, the value of the bound variable at the time of creation of the button).

NetBeans: accessing class from MyAppView.java

When I add an actionperformed event to a button, it generates the TODO section.
However, I would like for it to call a public method defined in MyApp.java, instead of having to define the method inside the MyAppView.java
How can I do this ? Tell MyAppView.java to get the methods and classes from MyApp.java ?
If I understand you correcty:
MyApp.java should implement ActionListener interface, then when creating button. Add object of the class MyApp to listen for actions on this button.
button.addActionListener(myApp);