当前位置: 技术问答>java相关
JTree是不是有问题?(在JTree中删除一个节点时)
来源: 互联网 发布时间:2015-01-06
本文导语: 在JTree中删除一个节点时,当时可以看到节点已经被删除掉了,可是当我把树合上,再次展开时,节点为什么还存在? 程序如下: TreePath path = selectionModel.getSelectionPath(); MutableTreeNode node = (MutableTreeNode)path.getLastPathComponent();...
在JTree中删除一个节点时,当时可以看到节点已经被删除掉了,可是当我把树合上,再次展开时,节点为什么还存在?
程序如下:
TreePath path = selectionModel.getSelectionPath();
MutableTreeNode node = (MutableTreeNode)path.getLastPathComponent();
model.removeNodeFromParent(node);
程序如下:
TreePath path = selectionModel.getSelectionPath();
MutableTreeNode node = (MutableTreeNode)path.getLastPathComponent();
model.removeNodeFromParent(node);
|
这是我修改过后的代码:
package UserManage;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.tree.*;
import java.util.*;
public class UserManageFrame extends JFrame
implements ActionListener,MenuListener{
private JPopupMenu popup;
private MouseAdapter msAdapter;
private JTree jTreeUser;
private JTree jTreeTest;
private static DefaultTreeModel model;
private TreeSelectionModel selectionModel;
private DefaultMutableTreeNode nodeChoosed; //记录当前被选中的节点的信息
private DefaultMutableTreeNode nodeGroup;
private DefaultMutableTreeNode nodeRole;
private Vector UserGroup; //记录所有用户组的信息
private Object[][] UserInGroup; //记录用户组中所有用户
private Vector Roles; //记录所有角色的信息
private JMenuItem addGroup;
private JMenuItem delGroup;
private JMenuItem modifyGroup;
private JMenuItem addUser;
private JMenuItem delUser;
private JMenuItem modifyUser;
private JMenuItem addRole;
private JMenuItem delRole;
private JMenuItem modifyRole;
private JMenuItem setRights;
public UserManageFrame() {
setTitle("权限管理系统");
setSize(400,300);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
//Construct the User Tree root Node.
DefaultMutableTreeNode root = new DefaultMutableTreeNode("权限管理");
jTreeUser = new JTree(root);
nodeGroup = new DefaultMutableTreeNode("用户组");
root.add(nodeGroup);
nodeRole = new DefaultMutableTreeNode("角色");
root.add(nodeRole);
jTreeUser = new JTree(root);
model = (DefaultTreeModel)jTreeUser.getModel();
selectionModel = jTreeUser.getSelectionModel();
selectionModel.setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
getAllGroupAndRole();
for(int i = 0;i
package UserManage;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.tree.*;
import java.util.*;
public class UserManageFrame extends JFrame
implements ActionListener,MenuListener{
private JPopupMenu popup;
private MouseAdapter msAdapter;
private JTree jTreeUser;
private JTree jTreeTest;
private static DefaultTreeModel model;
private TreeSelectionModel selectionModel;
private DefaultMutableTreeNode nodeChoosed; //记录当前被选中的节点的信息
private DefaultMutableTreeNode nodeGroup;
private DefaultMutableTreeNode nodeRole;
private Vector UserGroup; //记录所有用户组的信息
private Object[][] UserInGroup; //记录用户组中所有用户
private Vector Roles; //记录所有角色的信息
private JMenuItem addGroup;
private JMenuItem delGroup;
private JMenuItem modifyGroup;
private JMenuItem addUser;
private JMenuItem delUser;
private JMenuItem modifyUser;
private JMenuItem addRole;
private JMenuItem delRole;
private JMenuItem modifyRole;
private JMenuItem setRights;
public UserManageFrame() {
setTitle("权限管理系统");
setSize(400,300);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
//Construct the User Tree root Node.
DefaultMutableTreeNode root = new DefaultMutableTreeNode("权限管理");
jTreeUser = new JTree(root);
nodeGroup = new DefaultMutableTreeNode("用户组");
root.add(nodeGroup);
nodeRole = new DefaultMutableTreeNode("角色");
root.add(nodeRole);
jTreeUser = new JTree(root);
model = (DefaultTreeModel)jTreeUser.getModel();
selectionModel = jTreeUser.getSelectionModel();
selectionModel.setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
getAllGroupAndRole();
for(int i = 0;i