9

In my current project, I'm working on a rather simple JavaFX GUI containing a TreeTableView. To initialize the View I have the following code.

cmdNrColumn.setCellFactory(new Callback<TreeTableColumn<Command, Command>, TreeTableCell<Command, Command>>() {
    @Override public TreeTableCell<Command, Command> call(final TreeTableColumn<Command, Command> p) {
        return new TreeTableCell<Command, Command>() {
            @Override protected void updateItem(Command item, boolean empty) {
                super.updateItem(item, empty);

                TreeTableView treeTable = p.getTreeTableView();

                if (getIndex() >= treeTable.getExpandedItemCount()) {
                    setText(null);
                } else {
                    TreeItem<Command> treeItem = treeTable.getTreeItem(getIndex());
                    if (item == null || empty || treeItem == null || treeItem.getValue() == null) {
                        setText(null);
                    } else {
                        setText(Integer.toString(item.getCmdNr()));
                    }
                }
            }
        };
    }
});

Being new to Java 8 I'm not quite sure if and how this could be simplified to a Lambda expression.

Any help or tutorial on how to convert complicated and nested calls to a Lambda expression would be kindly appreciated

Thanks!

1 Answer 1

12

You can transform the outer anonymous class into a lambda expression:

cmdNrColumn.setCellFactory(p ->
    new TreeTableCell<Command, Command>() {
        @Override
        protected void updateItem(Command item, boolean empty) {
            // ...
            TreeTableView treeTable = p.getTreeTableView();
            // ...
        }
    });

However, the same transformation isn't possible for the inner anonymous class, because TreeTableCell is an abstract class, and lambda expressions can only be used for interfaces.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.