我收到错误消息:“从内部类引用的局部变量必须是最终的或实际上是最终的”
此错误指的是:
MyTreeItem LeftRootItem = (MyTreeItem) treeViewLeft.getRoot();
我的错误是什么。为什么这个错误不会像“treeViewRight”那样?
addAttribute.setOnAction(new EventHandler<ActionEvent>(){
@Override
public void handle(ActionEvent event) {
TreeView treeViewLeft = new TreeView ();
treeViewLeft.setShowRoot(false);
treeViewLeft.setRoot(new MyTreeItem ("root"));
treeViewLeft.getRoot().setExpanded(true);
TreeView treeViewRight;
Button button = new Button ("<<");
HBox hBox = new HBox ();
DBConnect.dbconnect();
treeViewRight = DB.loadAttributeClasses();
treeViewLeft = DB.getAttributesfromClassorProduct(selectedItem.getClassID());
treeViewRight.setShowRoot(false);
treeViewRight.getRoot().setExpanded(true);
hBox.getChildren().addAll(treeViewLeft, button, treeViewRight);
Scene scene = new Scene(hBox);
Stage attributeSelect = new Stage ();
attributeSelect.setTitle("Hello World!");
attributeSelect.setScene(scene);
attributeSelect.show();
button.setOnAction(new EventHandler<ActionEvent>(){
@Override
public void handle(ActionEvent event) {
MyTreeItem rightItem = (MyTreeItem) treeViewRight.getSelectionModel().getSelectedItem();
DB.setProdClassAttr(selectedItem.getClassID(), rightItem.getClassID(), selectedItem.getType());
rightItem.getParent().getChildren().remove(rightItem);
MyTreeItem LeftRootItem = (MyTreeItem) treeViewLeft.getRoot();
}
});
}
});
答案 0 :(得分:1)
问题是您将两个不同的TreeView
分配给treeViewLeft
。
你的第4行是
TreeView treeViewLeft = new TreeView ();
然后向下几行,为treeViewLeft
分配一个新值:
treeViewLeft = DB.getAttributesfromClassorProduct(selectedItem.getClassID());
如果您想从按钮treeViewLeft
处理程序引用onAction
,则不允许这样做。
您只需为treeViewRight
分配一次值,这是允许的。