我正在尝试为我的撤消功能删除一个元素,但我只想删除添加到 JTree 的最后一个东西。
对于删除,我只是在修改我的 Cut 方法:
private void performUndoAction() {
if (tree != null) {
TreePath path = tree.getSelectionPath();
if (path != null) {
DefaultMutableTreeNode node = (DefaultMutableTreeNode) path.getLastPathComponent();
if (node != tree.getModel().getRoot()) {
DefaultMutableTreeNode parent = (DefaultMutableTreeNode) node.getParent();
if (node.getUserObject() instanceof Attribute) {
Attribute at = (Attribute) node.getUserObject();
at.getParent().removeAttribute(at);
cut = at;
}
else if (node.getUserObject() instanceof Element) {
Element el = (Element) node.getUserObject();
el.getParent().removeContent(el);
cut = el;
} else if (node.getUserObject() instanceof Text) {
cut = ((Text) node.getUserObject()).getText();
Element el = (Element) parent.getUserObject();
el.setText("");
}
parent.remove(node);
tree.updateUI();
}
}
}
}