只能删除第三层上的节点, 删除节点后调整光标位置。 
最后一个子节点:将光标调整到父节点上
后有节点:将光标调整到后一节点上
后无节点:将光标调整到前一节点上
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.tree.*;
import java.awt.*;
import java.awt.event.*;public class Test extends JFrame {
JTree tree = new JTree(); DefaultTreeModel model = (DefaultTreeModel) tree.getModel(); TreeSelectionModel selectionModel = tree.getSelectionModel(); JButton removeButton = new JButton("Remove selected node"); JButton addButton = new JButton("Add node"); public Test() {
Container contentPane = getContentPane(); selectionModel
.setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION); contentPane.add(new ControlPanel(), BorderLayout.NORTH);
contentPane.add(tree, BorderLayout.CENTER); tree.addTreeSelectionListener(new TreeSelectionListener() {
public void valueChanged(TreeSelectionEvent e) {
TreePath path = e.getNewLeadSelectionPath();
boolean nodesAreSelected = (path != null); addButton.setEnabled(nodesAreSelected);
removeButton.setEnabled(nodesAreSelected);
}
});
model.addTreeModelListener(new TreeModelListener() {
public void treeNodesInserted(TreeModelEvent e) {
showInsertionOrRemoval(e, " added to ");
} public void treeNodesRemoved(TreeModelEvent e) {
showInsertionOrRemoval(e, " removed from ");
} private void showInsertionOrRemoval(TreeModelEvent e, String s) {
Object[] parentPath = e.getPath();
int[] indexes = e.getChildIndices();
Object[] children = e.getChildren();
Object parent = parentPath[parentPath.length - 1]; JOptionPane.showMessageDialog(Test.this, "Node \""
+ children[0].toString() + "\"" + s + parent.toString()
+ " at index " + indexes[0], "Node Added or Removed",
JOptionPane.INFORMATION_MESSAGE);
} public void treeNodesChanged(TreeModelEvent e) {
} public void treeStructureChanged(TreeModelEvent e) {
}
});
} class ControlPanel extends JPanel {
public ControlPanel() {
addButton.setEnabled(false);
removeButton.setEnabled(false); add(addButton);
add(removeButton); addButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
TreePath path = selectionModel.getSelectionPath(); MutableTreeNode parent, node = (MutableTreeNode) path
.getLastPathComponent();

if (path.getPathCount() > 1)
parent = (MutableTreeNode) node.getParent();
else
parent = (MutableTreeNode) node; int index = parent.getIndex(node) + 1; String s = JOptionPane.showInputDialog(Test.this,
"Enter a name for the new node:", "New Tree Node",
JOptionPane.QUESTION_MESSAGE); MutableTreeNode newNode = new DefaultMutableTreeNode(s); model.insertNodeInto(newNode, parent, index);
}
});
removeButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
TreePath path = selectionModel.getSelectionPath();
if (path.getPathCount() == 1) {
JOptionPane.showMessageDialog(ControlPanel.this,
"Can't remove root node!");
return;
}else if (path.getPathCount() == 2) { 
JOptionPane.showMessageDialog(ControlPanel.this,
"Can't remove node!");
return;
} MutableTreeNode node = (MutableTreeNode) path
.getLastPathComponent(); model.removeNodeFromParent(node);
}
});
}
} public static void main(String args[]) {
GraphicJavaApplication.launch(new Test(), "Tree Model Example", 300,
300, 450, 300);
}
}class GraphicJavaApplication extends WindowAdapter {
public static void launch(final JFrame f, String title, final int x,
final int y, final int w, int h) {
f.setTitle(title);
f.setBounds(x, y, w, h);
f.setVisible(true); f.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); f.addWindowListener(new WindowAdapter() {
public void windowClosed(WindowEvent e) {
System.exit(0);
}
});
}
}

解决方案 »

  1.   

    removeButton.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    TreePath path = selectionModel.getSelectionPath();

    DefaultMutableTreeNode node = (DefaultMutableTreeNode) path.getLastPathComponent();
    DefaultMutableTreeNode parentNode = (DefaultMutableTreeNode) node.getParent();
    TreePath newPath = null;
    TreeNode nextNode = parentNode.getChildAfter(node);
    TreeNode prevNode = parentNode.getChildBefore(node);
    if (nextNode != null) {
    newPath = path.getParentPath().pathByAddingChild(nextNode);
    }
    else if (prevNode != null) {
    newPath = path.getParentPath().pathByAddingChild(prevNode);
    }
    else {
    newPath = path.getParentPath();
    } if (path.getPathCount() == 1) {
    JOptionPane.showMessageDialog(ControlPanel.this,
    "Can't remove root node!");
    return;
    } else if (path.getPathCount() == 2) {
    JOptionPane.showMessageDialog(ControlPanel.this,
    "Can't remove node!");
    return;
    }
    model.removeNodeFromParent(node);

    tree.setSelectionPath(newPath);
    }
    });