我是javaFX的初学者,并且在这里使用TreeView
。我正在尝试使用鼠标事件,但这给了我一些我无法理解的错误。
Controller类
package application;
import java.net.URL;
import java.util.ResourceBundle;
import com.sun.glass.events.MouseEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeView;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
public class MainController implements Initializable{
@FXML
TreeView<String> treeView;
Image icon=new Image(
getClass().getResourceAsStream("/img/folder.png"));
@Override
public void initialize(URL arg0, ResourceBundle arg1) {
ImageView iconView=new ImageView(icon);
iconView.setFitWidth(25);
iconView.setFitHeight(25);
TreeItem<String> root=new TreeItem<>("Root", iconView);
//Set By Default Expanded
root.setExpanded(true);
TreeItem<String> nodeA=new TreeItem<>("Node A");
TreeItem<String> nodeB=new TreeItem<>("Node B");
TreeItem<String> nodeC=new TreeItem<>("Node C");
TreeItem<String> nodeD=new TreeItem<>("Node D");
//Expanded NodeA By Default
nodeA.setExpanded(true);
TreeItem<String> nodeA1=new TreeItem<>("Node A");
TreeItem<String> nodeB1=new TreeItem<>("Node B");
TreeItem<String> nodeC1=new TreeItem<>("Node C");
TreeItem<String> nodeD1=new TreeItem<>("Node D");
root.getChildren().add(nodeA);
root.getChildren().add(nodeB);
root.getChildren().add(nodeC);
root.getChildren().add(nodeD);
nodeA.getChildren().addAll(nodeA1,nodeB1,nodeC1,nodeD1);
treeView.setRoot(root);
}
public void mouseEvent(MouseEvent me) {
TreeItem<String> item=treeView.getSelectionModel().getSelectedItem();
System.out.println(item.getValue());
}
}
并且FXML文件代码为
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.TreeView?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane prefHeight="400.0" prefWidth="400.0" xmlns="http://javafx.com/javafx/9.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.MainController">
<children>
<TreeView fx:id="treeView" layoutX="54.0" layoutY="62.0" onContextMenuRequested="#mouseEvent" onMouseClicked="#mouseEvent" prefHeight="277.0" prefWidth="293.0" />
</children>
</AnchorPane>
其中在控制台中给出的错误为:
> Aug 26, 2018 11:36:11 AM javafx.fxml.FXMLLoader$ValueElement processValue
WARNING: Loading FXML document with JavaFX API of version 9.0.1 by JavaFX runtime of version 8.0.161
javafx.fxml.LoadException: Error resolving onMouseClicked='#mouseEvent', either the event handler is not in the Namespace or there is an error in the script.
/C:/Users/Al%20Fateh/eclipse-workspace/TreeView/bin/application/Main.fxml:8
at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2597)
at javafx.fxml.FXMLLoader.access$100(FXMLLoader.java:103)
at javafx.fxml.FXMLLoader$Element.processEventHandlerAttributes(FXMLLoader.java:610)
at javafx.fxml.FXMLLoader$ValueElement.processEndElement(FXMLLoader.java:770)
at javafx.fxml.FXMLLoader.processEndElement(FXMLLoader.java:2823)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2532)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3214)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097)
at application.Main.start(Main.java:15)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$161(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$174(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$172(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$173(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$147(WinApplication.java:177)
at java.lang.Thread.run(Unknown Source)