Convert Callback into Java 8 Lambda expression
java, java-8, javafx-8, lambda
Solution
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.
Problem
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!