user5182503
user5182503

Reputation:

JavaFx: TableView - unbind all properties

I have a TableView and the columns for it I create like this:

TableColumn<Foo,String> fieldColumn=new TableColumn("field");
fieldColumn.setCellValueFactory(data->data.getValue().getFieldProperty());
tableView.add(fieldColumn);

Now I want to destroy my tableView but I want to continue to use all foos. That's why I want to unbind foos' properties from table/column. How to do it?

Upvotes: 0

Views: 414

Answers (1)

fabian
fabian

Reputation: 82461

If you remove the TableColumns, the listeners will be removed during the next layout pass. This allows you to remove the listeners added by the TableView by clearing the columns and calling layout():

Item class for allowing to get the number of listeners to the property

public class Item {

    private final Set<Object> listeners = new HashSet<>();

    public Item(String value) {
        this.value.set(value);
    }

    private final StringProperty value = new SimpleStringProperty() {

        @Override
        public void removeListener(ChangeListener<? super String> listener) {
            super.removeListener(listener);
            listeners.remove(listener);
        }

        @Override
        public void addListener(ChangeListener<? super String> listener) {
            super.addListener(listener);
            listeners.add(listener);
        }

        @Override
        public void removeListener(InvalidationListener listener) {
            super.removeListener(listener);
            listeners.remove(listener);
        }

        @Override
        public void addListener(InvalidationListener listener) {
            super.addListener(listener);
            listeners.add(listener);
        }

    };

    public final StringProperty valueProperty() {
        return this.value;
    }

    public int getListenerCount() {
        return listeners.size();
    }

}

Text Application

private void printListenerCount(String message) {
    System.out.println(message + tableView.getItems().stream().mapToInt(Item::getListenerCount).sum());
}

private TableView<Item> tableView;

@Override
public void start(Stage primaryStage) {
    tableView = new TableView<>();
    tableView.getItems().addAll(new Item("a"), new Item("b"), new Item("c"));

    TableColumn<Item, String> column = new TableColumn<>();
    column.setCellValueFactory(cd -> cd.getValue().valueProperty());

    tableView.getColumns().add(column);

    Button btn = new Button("print listener count");
    btn.setOnAction((ActionEvent event) -> {
        printListenerCount("listeners: ");
    });

    Button btn2 = new Button("clear columns");
    btn2.setOnAction(evt -> {
        tableView.getColumns().clear();

        // do layout to remove the listeners added for the columns
        tableView.layout();

        printListenerCount("after clear columns: ");
    });

    Scene scene = new Scene(new VBox(tableView, btn, btn2));

    primaryStage.setScene(scene);
    primaryStage.show();
}

Pressing the print listener count and then the clear columns buttons will result in the following output:

listeners: 3
after clear columns: 0

Upvotes: 0

Related Questions