如何创建JTree ExpansionListener

时间:2015-11-11 11:27:40

标签: java events jtree defaultmutabletreenode

我在创建TreeSelectionListener时遇到问题。我认为它可以像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); }; } 一样简单。不幸的是我找不到正确的界面。

这是我的听众:

TreeSelectionListener

我收到的错误是“不兼容的类型:TreeExpansionListener不是一个功能接口。在TreeExpansionListener中找到多个非重写抽象方法”

与我的 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); }; } 比较:     private TreeSelectionListener createSelectionListener(){         return(TreeSelectionEvent e) - > {

$host="localhost";
$username="root";
$password="demo"; 
$db_name="test";
$tbl_name="test";

$mysqli = new mysqli("localhost",$username,$password,$db_name);

$sql="SELECT * FROM $tbl_name";

$res=$mysqli->query($sql);


    while($rows=$res->fetch_assoc()){ ?>
         <table width="400" border="1" cellspacing="0" cellpadding="3">
            <tr>
              <td width="10%"><? echo $rows['id']; ?></td>
              <td width="30%"><? echo $rows['name']; ?></td>
              <td width="30%"><? echo $rows['lastname']; ?></td>
              <td width="30%"><? echo $rows['email']; ?></td>
            </tr>
         </table>
   <?php }
         $res->close();
  ?>

我是否认为这一切都错了?或者我错过了一些明显的东西?

1 个答案:

答案 0 :(得分:1)

您无法在非功能性界面上使用lambda表达式。功能界面只有一个抽象方法。

TreeExpansionListener

TreeSelectionListener

private TreeExpansionListener createExpansionListener() {
   return new TreeExpansionListener(){
   // implement methods and do stuff...
   }
}