【问题标题】:track change of nodes bound in javafx跟踪 javafx 中绑定的节点的变化
【发布时间】:2015-07-22 13:10:26
【问题描述】:

我想要这篇文章中要求的同样的东西 track changes of nodes bound in JavaFX

我有这样的事情: (已编辑) 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);
}

}

但问题是更改的方法没有正确调用......它只被调用了一次......如果我在更改的方法而不是旧的方法中添加这个代码它被正确调用......但它是非常非常慢

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

谁能帮帮我? 谢谢...

【问题讨论】:

    标签: java javafx javafx-2 javafx-8


    【解决方案1】:

    由于本例中的ButtonCircle 位于同一个父窗格中,显然您可以通过将圆的位置绑定到按钮的boundsInParentProperty() 来更简单(有效)地完成此操作。我假设您这样做是因为您有一个真实的应用程序,其中两个节点位于不同的父节点中。

    看起来localToSceneTransformProperty() 正在过早地收集垃圾(另请参阅this)。您可以使用侦听器而不是绑定来解决此问题:

    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);
    
        }
    
    }
    

    【讨论】:

    • 谢谢你,但不幸的是它不起作用!你还有其他想法吗?
    • 相应地更新了答案。
    • 非常感谢...它可以工作...是的,我有一个具有许多节点的真实应用程序,这是一个非常简单的示例...您可以解释一下如何理解哪个是垃圾收集到了?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-03-31
    • 1970-01-01
    • 2013-03-02
    • 1970-01-01
    • 2011-03-11
    • 2015-06-02
    • 2019-01-17
    相关资源
    最近更新 更多