I am very new to RxJava,
I just want to ask if RxJava has any alternative feature to JavafX Observable Change Listener. So I want to implement something like below with RxJava. I hope I am not wrong about Observable in RxJava and its similar to JavaFx..
ObservableList ov = new ....
ov .addListener(new ChangeListener<String>() {
@Override
public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
if (!newValue.matches("\\d*")) {
textField.setText(newValue.replaceAll("[^\\d]", ""));
}
if (newValue.isEmpty())
textField.setText("0");
}
});
The closest thing would be to use PublishSubject and scan, possibly with distinctUntilChanged to filter out subsequent duplicates.
var subject = PublishSubject.create();
subject
.distinctUntilChanged()
.scan((prev, current) -> {
// process change
return current;
})
.subscribe(...);
subject.onNext("New value");
subject.onNext("New value2");
Thanks,
Do you think RxJava is a good replacement for FXObservable? Does my question even make sense?
RxJava can do a lot more, but you'd need to bridge it back to JavaFX constructs. There is the https://github.com/ReactiveX/RxJavaFX project but I don't know where they are at the moment regarding version support.
@akarnokd, Thanks for very quick responses.
I think I did not explain the issue well - I am using JavaFx in my java console app that has no GUI. Just because of the Property and addlistener of obervablelist. And I dont need JavaFX actually if I have a better way to fire a function when a change happens in a list,variabl, etc.
So I was searching for replacement of JavaFX Observable then I found your project.
So in conclusion, there is no UI exists, can I use FxJava to implement a change for a List
Im not expert programmer so any other hint appreaciated.
You'd have to write such functionality on top of a data structure with the tools I mentioned above.
Yes I tried that, but it does not replicate same functionality.
Currently what I have is a ObservableList<Test> testOV then attached a testOV.addlistener() to it to trigger a function when a changes happens, also testOV can list down the items that it stores.
By using the below, I can't list the items in the subject, or remove any item.
PublishSubject<Test> subject = PublishSubject.create();
subject
.distinctUntilChanged()
.subscribe(s -> System.out.println(s.getId()));
subject.onNext(new TestBuilder().withId("1111").build());
subject.onNext(new TestBuilder().withId("2222").build());
The subject is there for dispatching notifications, you'll still need a list to store the items.
Here is an example about listening to hashmap changes: https://stackoverflow.com/a/59834853/61158