1 Reply Latest reply on Feb 5, 2013 4:10 PM by James_D

    Bind Parent to a Observable List

      I want to bind the VBox.getChildren() ObservableList to my own ObservableList. So that, when ever my process detects an Image, it is added to my list which in turn automatically is added to the VBox.

      It throws the below exception:

      Exception in thread "Thread-3" java.lang.UnsupportedOperationException

      Is there any other way of doing it? What is the issue above?
        • 1. Re: Bind Parent to a Observable List
          UnsupportedOperationExceptions usually result from trying to modify an unmodifiable collection; so my guess in this case is that somehow your list is unmodifiable. The general concept works ok:
          import javafx.application.Application;
          import javafx.beans.binding.Bindings;
          import javafx.collections.FXCollections;
          import javafx.collections.ObservableList;
          import javafx.event.ActionEvent;
          import javafx.event.EventHandler;
          import javafx.scene.Node;
          import javafx.scene.Scene;
          import javafx.scene.control.Label;
          import javafx.scene.control.TextField;
          import javafx.scene.layout.BorderPane;
          import javafx.scene.layout.VBox;
          import javafx.stage.Stage;
          public class BoundChildList extends Application {
            public void start(Stage primaryStage) {
              final BorderPane root = new BorderPane();
              final VBox vbox = new VBox(5);
              final ObservableList<Node> nodes = FXCollections.observableArrayList();
              Bindings.bindContentBidirectional(vbox.getChildren(), nodes);
              final TextField textField = new TextField();
              textField.setOnAction(new EventHandler<ActionEvent>() {
                public void handle(ActionEvent event) {
                  nodes.add(new Label(textField.getText()));
              Scene scene = new Scene(root, 400, 600);
            public static void main(String[] args) {
          You may also end up with exceptions here if you add something to your custom list that is already part of a scene graph, or create "cycles" in the scene graph; but in these cases you would get an IllegalArgumentException.

          One other thing: establishing the binding and modifying your list (once the binding is established) will change the scene graph. So you need to make sure any of those operations are done on the FXApplicationThread. It looks from your error message that you're doing this on another thread; though that won't cause the exception you're seeing.

          Edited by: James_D on Feb 5, 2013 8:07 AM
          1 person found this helpful