Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javafx - how to disable events fired not from the user

Tags:

events

javafx

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!!!


2 Answers

You can temporarily remove the listener and add it again:

guiPageList.getSelectionModel().selectedItemProperty().removeListener(pageItemSelected);
guiPageList.setValue(model.getCurrentTargetPage());
guiPageList.getSelectionModel().selectedItemProperty().addListener(pageItemSelected);
like image 175
Uluk Biy Avatar answered Feb 02 '26 04:02

Uluk Biy


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.

like image 32
Andy Till Avatar answered Feb 02 '26 06:02

Andy Till



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!