gpt4 book ai didi

java - 如何测试树项是否是javafx中 TreeView 的根节点?

转载 作者:行者123 更新时间:2023-12-01 11:41:39 24 4
gpt4 key购买 nike

我一直试图阻止我的程序删除 TreeView 的根节点,但是每当我尝试这样做时,我的程序都会在编译过程中给我一个“java.lang.reflect.InitationTargetException”,并且程序不会发射。我不太确定为什么它不会启动。

这是发生错误的 TextFieldTreeCellImpl 类:

private final class TextFieldTreeCellImpl extends TreeCell<String> {

private TextField textField;
private ContextMenu addMenu = new ContextMenu();


public TextFieldTreeCellImpl() {
MenuItem addMenuItem0 = new MenuItem("Add Folder");
addMenu.getItems().add(addMenuItem0);
addMenuItem0.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent t) {
TreeItem<String> newFolder =
new TreeItem<String>("New Folder",new ImageView(depIcon));
getTreeItem().getChildren().add(newFolder);
}
});

MenuItem addMenuItem1 = new MenuItem("Delete");
addMenu.getItems().add(addMenuItem1);

addMenuItem1.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent t) {//There is not error when the if statement is in here
getTreeItem().getParent().getChildren().remove(getTreeItem());
}
});
if(getTreeItem().getParent()==null){//The error is here-------------
addMenuItem1.disableProperty().set(true);
}
}

public void startEdit() {
super.startEdit();

if (textField == null) {
createTextField();
}
setText(null);
setGraphic(textField);
textField.selectAll();
}

public void cancelEdit() {
super.cancelEdit();

setText((String) getItem());
setGraphic(getTreeItem().getGraphic());
}


public void updateItem(String item, boolean empty) {
super.updateItem(item, empty);

if (empty) {
setText(null);
setGraphic(null);
} else {
if (isEditing()) {
if (textField != null) {
textField.setText(getString());
}
setText(null);
setGraphic(textField);
} else {
setText(getString());
setGraphic(getTreeItem().getGraphic());
setContextMenu(addMenu);
}
}
}

private void createTextField() {
textField = new TextField(getString());
textField.setOnKeyReleased(new EventHandler<KeyEvent>() {

public void handle(KeyEvent t) {
if (t.getCode() == KeyCode.ENTER) {
commitEdit(textField.getText());
} else if (t.getCode() == KeyCode.ESCAPE) {
cancelEdit();
}
}
});

}

private String getString() {
return getItem() == null ? "" : getItem().toString();
}
}
}

这是我得到的错误:

Exception in Application start method
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(Unknown Source)
at com.sun.javafx.application.LauncherImpl.launchApplication(Unknown Source)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at sun.launcher.LauncherHelper$FXHelper.main(Unknown Source)
Caused by: java.lang.RuntimeException: Exception in Application start method
at com.sun.javafx.application.LauncherImpl.launchApplication1(Unknown Source)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$147(Unknown Source)
at com.sun.javafx.application.LauncherImpl$$Lambda$48/128893786.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.NullPointerException
at application.CopyOfMain$TextFieldTreeCellImpl.<init>(CopyOfMain.java:82)
at application.CopyOfMain$1.call(CopyOfMain.java:48)
at application.CopyOfMain$1.call(CopyOfMain.java:1)
at com.sun.javafx.scene.control.skin.TreeViewSkin.createCell(Unknown Source)
at com.sun.javafx.scene.control.skin.TreeViewSkin.lambda$new$513(Unknown Source)
at com.sun.javafx.scene.control.skin.TreeViewSkin$$Lambda$115/979660402.call(Unknown Source)
at com.sun.javafx.scene.control.skin.VirtualFlow.getCell(Unknown Source)
at com.sun.javafx.scene.control.skin.VirtualFlow.getCellLength(Unknown Source)
at com.sun.javafx.scene.control.skin.VirtualFlow.computeViewportOffset(Unknown Source)
at com.sun.javafx.scene.control.skin.VirtualFlow.layoutChildren(Unknown Source)
at javafx.scene.Parent.layout(Unknown Source)
at javafx.scene.Parent.layout(Unknown Source)
at javafx.scene.Parent.layout(Unknown Source)
at javafx.scene.Scene.doLayoutPass(Unknown Source)
at javafx.scene.Scene.preferredSize(Unknown Source)
at javafx.scene.Scene.impl_preferredSize(Unknown Source)
at javafx.stage.Window$9.invalidated(Unknown Source)
at javafx.beans.property.BooleanPropertyBase.markInvalid(Unknown Source)
at javafx.beans.property.BooleanPropertyBase.set(Unknown Source)
at javafx.stage.Window.setShowing(Unknown Source)
at javafx.stage.Window.show(Unknown Source)
at javafx.stage.Stage.show(Unknown Source)
at application.CopyOfMain.start(CopyOfMain.java:54)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$153(Unknown Source)
at com.sun.javafx.application.LauncherImpl$$Lambda$57/1520696636.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$166(Unknown Source)
at com.sun.javafx.application.PlatformImpl$$Lambda$44/1051754451.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$null$164(Unknown Source)
at com.sun.javafx.application.PlatformImpl$$Lambda$47/1933975749.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$165(Unknown Source)
at com.sun.javafx.application.PlatformImpl$$Lambda$46/1775282465.run(Unknown Source)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(Unknown Source)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$141(Unknown Source)
at com.sun.glass.ui.win.WinApplication$$Lambda$37/1109371569.run(Unknown Source)
... 1 more
Exception running application application.CopyOfMain

这是我的主类,尽管我不认为它是真实的:

public class Main extends Application {

private final Node rootIcon =
new ImageView(new Image(getClass().getResourceAsStream("folder.jpg")));
private final Image depIcon =
new Image(getClass().getResourceAsStream("folder.jpg"));

TreeItem<String> rootNode =
new TreeItem<String>("Resources", rootIcon);

public static void main(String[] args) {
Application.launch(args);
}

public void start(Stage stage) {
rootNode.setExpanded(true);
stage.setTitle("Tree View Sample");
VBox box = new VBox();
final Scene scene = new Scene(box, 400, 300);
scene.setFill(Color.LIGHTGRAY);

TreeView<String> treeView = new TreeView<String>(rootNode);
treeView.setEditable(true);
treeView.setCellFactory(new Callback<TreeView<String>,TreeCell<String>>(){
public TreeCell<String> call(TreeView<String> p) {
return new TextFieldTreeCellImpl();
}
});

box.getChildren().add(treeView);
stage.setScene(scene);
stage.show();
}

我确实注意到的一件事是,如果我将 if 语句放入其中一个句柄方法中,则不会出现错误。

最佳答案

在构造 TextFieldTreeCellImpl 对象时,没有与其关联的 TreeItem。这通常会在施工后不久发生,并且超出您的控制范围。无论如何,对 getTreeItem() 的调用将返回 null,因此对 getParent() 的链式调用将导致 NPE。当您将其移动到处理程序内时它起作用的原因是因为该代码仅在触发事件(即单击 addMenuItem1)时调用,并且到那时,treeItem填充在单元格中。

我会为 ContextMenuonShowing 事件添加另一个 EventHandler 并将检查和设置放在其中。请参阅文档中的示例:ContextMenu Javadoc

关于java - 如何测试树项是否是javafx中 TreeView 的根节点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29474937/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com