javafx - how to disable events fired not from the user


Question

i have a little problem with javafx. i added a change listener like this:

private final ChangeListener<String> pageItemSelected = new ChangeListener<String>()
{
@Override
public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue){
    pageGotSelected(newValue);
}
};

now to the problem: if i change an page item like this:

 guiPageList.setValue(model.getCurrentTargetPage());

the event gets also(as it get by selecting something with the mouse or key) fired. is there a way to disable the event firing or another way? i need the event only, if the element got selected by the user and not if i change it with the setValue() function... perhaps consuming the event, but i donĀ“t know what kind of event this would be.

thanks in advance!!!

1
1
9/25/2012 9:23:23 AM

Accepted Answer

You can temporarily remove the listener and add it again:

guiPageList.getSelectionModel().selectedItemProperty().removeListener(pageItemSelected);
guiPageList.setValue(model.getCurrentTargetPage());
guiPageList.getSelectionModel().selectedItemProperty().addListener(pageItemSelected);
8
9/25/2012 10:24:50 AM

Alternatively you could decorate the listener with another listener implementation, the code would be something like:

class InvalidationListenerEventBlocker implements InvalidationListener {
    InvalidationListener decoratedListener;
    boolean block;
    public void invalidated(Observable observable) {
        if(!block) {
            decoratedListener.invalidated(observable);
        }
    }
}

Add a setter for the block boolean and send the listener in through the constructor. Set block to true to stop events.


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon