I am having trouble creating a TreeExpansionListener
. I thought it could be as easy as a TreeSelectionListener
. Unfortunately I cannot find the correct interface.
Here is my listener:
private TreeExpansionListener createExpansionListener() {
return (TreeExpansionEvent event) -> {
TreePath path = event.getPath();
DefaultMutableTreeNode node = (DefaultMutableTreeNode) path.getLastPathComponent();
String data = node.getUserObject().toString();
System.out.println("Expanded: " + data);
};
}
The error I receive is "incompatible types: TreeExpansionListener is not a functional interface. multiple non-overriding abstract methods found in TreeExpansionListener"
compaired to my TreeSelectionListener
:
private TreeSelectionListener createSelectionListener() {
return (TreeSelectionEvent e) -> {
node = (DefaultMutableTreeNode) template_tree.getLastSelectedPathComponent();
if ((node == null){
return;
} else {
DefaultMutableTreeNode node = (DefaultMutableTreeNode) path.getLastPathComponent();
String data = node.getUserObject().toString();
System.out.println("Expanded: " + data);
};
}
Am I going about this all wrong? Or am I missing something obvious?
You can't use a lambda expression on a non-functional interface. A functional interface has only one abstract method.
private TreeExpansionListener createExpansionListener() {
return new TreeExpansionListener(){
// implement methods and do stuff...
}
}