8

I want same thing that asked in this post track changes of nodes bound in JavaFX

I have something like this : (Edited) SSCCE:

public class FloatCircle {

Node node;


Circle rectangle;
Bounds localToScreen;
static ArrayList<ObjectBinding> list = new ArrayList<>();
private ObjectBinding<Bounds> boundsInScene ;
Pane root ;

public FloatCircle(Node node,Pane root) {
    this.node = node;
   this.root =root;
    this.rectangle = new Circle();
    this.rectangle.setManaged(false);

    initSetting();

}



public Circle getFloatCircle() {
    return rectangle;
}

public void initSetting() {

   boundsInScene = Bindings.createObjectBinding(
            () -> node.localToScene(node.getBoundsInLocal()),
            node.localToSceneTransformProperty(),
            node.boundsInLocalProperty());
    boundsInScene.addListener(new ChangeListener<Bounds>() {

        @Override
        public void changed(ObservableValue<? extends Bounds> observable, Bounds oldValue, Bounds newValue) {
             setLocation(newValue);
            System.err.println("CHANGED!!!!!!");
        }

    });
    localToScreen = node.localToScene(node.getBoundsInLocal());
    setLocation(localToScreen);
    addCircle();
}

public void setLocation(Bounds localToScreen) {
    int r = 10;
            rectangle.setCenterX(localToScreen.getMinX() - r);
            rectangle.setCenterY(localToScreen.getMinY() - 5);
            rectangle.setRadius(r);

    //    return rect;

}

public void addCircle(){
            root.getChildren().add(rectangle);

}

}




public class SelfContained extends Application {

ArrayList<Node> nodes = new ArrayList<>();

@Override
public void start(Stage primaryStage) {
    Button btn = new Button();

    StackPane root = new StackPane();
    root.getChildren().add(btn);
    nodes.add(btn);

    Scene scene = new Scene(root, 300, 250);

    primaryStage.setTitle("Hello World!");
    primaryStage.setScene(scene);
    primaryStage.show();

    for (int i = 0; i < nodes.size(); i++) {
        Node n = nodes.get(i);
        FloatCircle floatCircle = new FloatCircle(n, root);

    }
}

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    launch(args);
}

}

but the problem is changed method doesn't called correctly...It is called just one time....if I add this code in changed method instead of the old one It is called correctly...But it is very very slow

           FloatCircle fc = new FloatCircle(node);
           fc.rectangle = rectangle ;
           fc.setLocation(newValue, directionID, alignment);

Can anyone help me? Thank's...

Community
  • 1
  • 1
faraa
  • 575
  • 3
  • 14
  • 42

1 Answers1

4

Since the Button and the Circle in this example are in the same parent pane, you could obviously do this much more simply (and efficiently) just by binding the circle's location to the button's boundsInParentProperty(). I assume that you are doing it this way because you have a real application in which the two nodes are in different parents.

It looks like the localToSceneTransformProperty() is getting garbage collected prematurely (see also this). You can fix this using a listener instead of a binding:

public static class FloatCircle {

    Node node;

    Circle rectangle;
    Pane root;

    public FloatCircle(Node node, Pane root) {
        this.node = node;
        this.root = root;
        this.rectangle = new Circle();
        this.rectangle.setManaged(false);

        initSetting();

    }

    public Circle getFloatCircle() {
        return rectangle;
    }

    public void initSetting() {

        ChangeListener<Object> updater = (obs, oldValue, newValue) -> 
            setLocation(node.localToScene(node.getBoundsInLocal()));
        node.boundsInLocalProperty().addListener(updater);
        node.localToSceneTransformProperty().addListener(updater);

        setLocation(node.localToScene(node.getBoundsInLocal()));
        addCircle();
    }

    public void setLocation(Bounds localToScreen) {
        int r = 10;
        rectangle.setCenterX(localToScreen.getMinX() - r);
        rectangle.setCenterY(localToScreen.getMinY() - 5);
        rectangle.setRadius(r);
    }

    public void addCircle() {
        root.getChildren().add(rectangle);

    }

}
Community
  • 1
  • 1
James_D
  • 201,275
  • 16
  • 291
  • 322
  • Thank you but unfortunately it doesn't work!Do you have any other idea ? – faraa Jul 22 '15 at 15:34
  • Updated answer accordingly. – James_D Jul 22 '15 at 17:55
  • Thank you so much...It works...yes I have a real application wit many nodes and it was a very simple example...And May you explain How to understand which one is garbage collected ? – faraa Jul 22 '15 at 18:13