Just lost my last not-gray hair while quick-testing notification behaviour, the guts being binding a local (to the init method) property to the rawTextProperty of a textBox
/*
* Created on 06.07.2011
*
*/
package bug;
import javafx.application.Application;
import javafx.beans.property.StringProperty;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.scene.Scene;
import javafx.scene.control.TextBox;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
// bind rawTextProperty
public class TextRawTextBind2 extends Application {
public static void main(String[] args) {
Application.launch(args);
}
private HBox createParent() {
TextBox modelInput = new TextBox();
StringProperty property = new StringProperty("some text");
ChangeListener l = new ChangeListener() {
@Override
public void changed(ObservableValue arg0, Object arg1, Object arg2) {
System.out.println("from adapter: " + arg1 + arg2
+ arg0.getClass());
}
};
property.addListener(l);
modelInput.rawTextProperty().bindBidirectional(property);
HBox hBox = new HBox();
hBox.getChildren().addAll(modelInput);
return hBox;
}
@Override
public void start(Stage primaryStage) throws Exception {
HBox hBox = createParent();
Scene scene = new Scene(hBox);
primaryStage.setScene(scene);
primaryStage.setVisible(true);
}
}
when typing into the textBox, I expect the listener to be notified - but nothing happens. Inline the createParent into the start method - the listener gets notified as expected (for your convenience, added that as well below).
So what's the difference? The only thingy I can think of (after a whole afternoon of extensive testing of my code as well as textBox .. ) might be listener management in the binding: if the bound property is kept by a weak reference, then it might get garbage collected on method exit. Don't know though, never really cared much about weak references ...
Comments please?
Cheers
Jeanette
here's the same as all-in-one
/*
* Created on 06.07.2011
*
*/
package bug;
import javafx.application.Application;
import javafx.beans.property.StringProperty;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.scene.Scene;
import javafx.scene.control.TextBox;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
// bind rawTextProperty
public class TextRawTextBind extends Application {
public static void main(String[] args) {
Application.launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
TextBox modelInput = new TextBox();
StringProperty property = new StringProperty("some text");
ChangeListener l = new ChangeListener() {
@Override
public void changed(ObservableValue arg0, Object arg1, Object arg2) {
System.out.println("from adapter: " + arg1 + arg2 + arg0.getClass()
);
}
};
property.addListener(l);
modelInput.rawTextProperty().bindBidirectional(property);
HBox hBox = new HBox();
hBox.getChildren().addAll(modelInput);
Scene scene = new Scene(hBox);
primaryStage.setScene(scene);
primaryStage.setVisible(true);
}
}