lundi 2 mai 2016

How to repeatedly update an instance variable as from `MouseEvent` javafx?

I am using the Observer patter, and i would like to be able to update all observers that the xValue of the subject class has change, using the xValue from the MouseEvent so basically when the user clicks on the scenethe xCompchanges and the observer, or in this case the Receivernotices the change and does something depending on the change. I can add the code for the observers if needed. Thank you for your time.

package subject;
import java.util.ArrayList;

import //all things needed
import observer.Observer;

public class Sender extends Application implements Subject {
    private double xComp;
    private double yComp;

    private ArrayList<Observer> observers;
    //constructor
    public double getxComp() {
        return xComp;
    }

    //accessors
    public void setxComp(double xComp) {
        this.xComp = xComp;
    }

    public double getyComp() {
        return yComp;
    }
    public void setxyComp(double xComp, double yComp) {
        this.yComp = yComp;
        this.xComp=xComp;
    }
    public void setyComp(double yComp) {
        this.yComp = yComp;
    }

    public Sender() {
        observers= new ArrayList<Observer>();
    }

    @Override
    public void addObserver(Observer o) {
        observers.add(o);

    }

    @Override
    public void removeObserver(Observer o) {
        int index= observers.indexOf(o);
        observers.remove(index);

    }
    @Override
    public void notifyObserver() {
        for (Observer o: observers)
            o.update(this.getxComp(), this.getyComp());
    }
    //GUI
        public void start(Stage s) throws Exception {

            TextField tf = new TextField("no");
            Group root =new Group();
            root.getChildren().add(tf);
            Scene scene = new Scene(root, 500,500);
            scene.setOnMouseClicked(new EventHandler <MouseEvent>(){

                @Override
                public void handle(MouseEvent e) {
                    Here

                }

            });
            s.setScene(scene);
            s.show();



        }

}

Aucun commentaire:

Enregistrer un commentaire