Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

Bug or feature: "weak" bidirectional binding?

kleopatra-JavaNetJul 11 2011 — edited Jul 14 2011
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);
    }
  }

Comments

Locked Post
New comments cannot be posted to this locked post.

Post Details

Locked on Aug 11 2011
Added on Jul 11 2011
3 comments
300 views