bind To JavaFX Stage Title - Java JavaFX

Java examples for JavaFX:Stage

Description

bind To JavaFX Stage Title

Demo Code


    //package com.java2s;

    import javafx.beans.property.StringProperty;
    import javafx.beans.value.ChangeListener;

    import javafx.scene.Node;

    import javafx.stage.Stage;
    import javafx.stage.Window;

    public class Main {
        public static void bindToStageTitle(Node n, StringProperty property) {

    ChangeListener<Window> windowListener = (observable, oldValue, newValue) -> {
        if (oldValue != null && oldValue instanceof Stage) {
            ((Stage) oldValue).titleProperty().unbindBidirectional(property);
        }//from   ww  w.ja  va 2s . com
        if (newValue != null && newValue instanceof Stage) {
            ((Stage) newValue).titleProperty().bindBidirectional(property);
        }
    };

    n.sceneProperty().addListener((observable, oldValue, newValue) -> {
        if (oldValue != null) {
            oldValue.windowProperty().removeListener(windowListener);
        }
        if (newValue != null) {
            newValue.windowProperty().addListener(windowListener);
            if (newValue.getWindow() != null && newValue.getWindow() instanceof Stage) {
                ((Stage) newValue.getWindow()).titleProperty().bindBidirectional(property);
            }
        }
    });
    if (n.getScene() != null) {
        n.getScene().windowProperty().addListener(windowListener);
    }
}
    }

Related Tutorials