I would like to feel comfortable with the MVC concept but what I am lacking therefore is the link between the Model and the View. All the Information on this Topic seem to be based on events to happen and to be handled but what I need is a link which is free from any events and in the same time from the Controler.
Here is my simple Model which is changing data without any Events:
package test12;
import java.util.Observable;
public class Model extends Observable implements Runnable {
static int i=0;
@Override
public void run() {
while (true ==true) {
i++;
setChanged();
notifyObservers(i);
}
}
public void start(){
Thread thread= new Thread(this);
thread.start();
}
public static int getI() {
return i;
}
}
Here is the View (FirstStage):
package test12;
import java.util.Observable;
import java.util.Observer;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
public class FirstStage extends Stage implements Observer{
Label l = new Label("The current value of i is: ");
TextField t = new TextField();
HBox x = new HBox();
public FirstStage(){
x.getChildren().addAll( l, t);
this.setScene(new Scene(x, 600, 300));
this.show();
}
@Override
public void update(Observable arg0, Object arg1) {
System.out.println("hallo"+arg1);
}
}
And finally we have the Main Class:
package test12;
import java.util.Observable;
import javafx.application.Application;
import javafx.stage.Stage;
public class Main extends Application {
Model model;
@Override
public void start(Stage primaryStage) {
FirstStage firstStage = new FirstStage();
model.addObserver(firstStage);
}
public static void main(String[] args) {
Model model = new Model();
model.start();
launch(args);
}
}
My ultimate Goal is to bind the value of the Label from FirstStage (View) with the Parameter i from the Model but unfortunatelly I am already failing with printing out the current value of i in the System.out... command. The reason for my failure is a NullPointerException in the Main class. The Problem was there as I put the Generation in the main method and as, as now I put it in the run method.
How can I solve this NullPointerException?
And how can I ultimatedly bind the Label with i value?
Kind regards, a student