1

I'm developing a simple Java based MCU simulation for the Pic16F84 µC. This simulator is divided into two parts:

  • The µC simulation (Memory, CPU, ALU, etc.)
  • The Graphical User Interface (JavaFX) for displaying memory content etc.

How could I connect the simulator with the UI in a reactive way? I want to develop the µC simulation as JavaFX independent unit. This means I don't want to use JavaFX related property classes as class fields inside of the simulator's classes.

For example let's say I've the following controller class bound to a FXML view:

public class SimulatorController implements Initializable {

    @FXML private Label memoryContent;
    private Pic16F84Simulator simulator;

    ...

    public SimulatorController() {

        simulator.getMemory().addPropertyChangeListener(event -> {

            memoryContent.setText((String) event.getValue());
        }
    }
}

I'm using PropertyChangeListeners for updating reactive. Of course most of the time the listener logic isn't that simple. So the code base grows very fast.

Is there a more elegant way connecting a JavaFX UI with a UI independent back-end class? With independent I mean I could extract the simulator implementation without any remaining JavaFX dependencies.

0x1C1B
  • 1,204
  • 11
  • 40
  • It's not clear to me what you mean with backend, and what are the constraints wit the `Pic16F84`. Is their any communication protocol? Is there a specific java API? – pdem Jun 19 '19 at 08:58
  • No sorry there isn't an API in the sense of network access. I just want to separate the simulator from to GUI logic. I want to be able to extract the simulator implementation and connect it let's say to the command line instead of JavaFX without need of modification. Basically decoupled "backend" unit but part of the same application – 0x1C1B Jun 19 '19 at 09:04

1 Answers1

3

If you want your app to be reactive, consider using RxJava in backend (there are, however, other reactive libraries, Comparison of Java reactive frameworks)

I suggest you to use RxJava (http://reactivex.io/, introduction). It is pretty difficult at the beginning, but once you get it, you will have an access to this library across quite a few platforms (ruby, javascript, java etc., http://reactivex.io/languages.html)

The most important thing you will have to learn about is an Observable (generic docs | java docs). Then just implement listeners using RxJava api (most probably you will want to use subscribe method) similarly to the code snippet you have sent.

Some blogs to read:

eeqk
  • 3,492
  • 1
  • 15
  • 22
  • Yea I already noticed this framework. So basically it's a more advanced alternative to the `PropertyChangeSupport` right? – 0x1C1B Jun 19 '19 at 09:06
  • I'm not really into JavaFx, but if you can pass variables to the view layer then it will do the job very well. – eeqk Jun 19 '19 at 09:13