首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

寻早JTree高手————在JTree的节点上点右键弹出菜单,解决有关问题马上给分

2012-02-03 
寻早JTree高手————在JTree的节点上点右键弹出菜单,解决问题马上给分!在线等这是源码需要做的是右键点击在

寻早JTree高手————在JTree的节点上点右键弹出菜单,解决问题马上给分!在线等
这是源码   需要做的是   右键点击在线好友和不在线的好友的子节点能弹出PopupMenu
有什么问题   马上题
public   class   FriendTree   implements   TreeSelectionListener
{
private   DefaultTreeModel   treemode;
private   String   nodenameAll;
private   DefaultMutableTreeNode   root=new   DefaultMutableTreeNode( "我的好友 ");
private   JTree   tree=new   JTree(root);

private   DefaultMutableTreeNode   online   ;
private   DefaultMutableTreeNode   notonline;
private   Client   client=new   Client();
FriendTree(Client   client)
{

this.client=client;

}
public   JTree   chushi()//未加任何好友的情况
{

treemode=(DefaultTreeModel)tree.getModel();//得到DefaultTreeModel
online   =new     DefaultMutableTreeNode( "在线的好友 ");
notonline   =new     DefaultMutableTreeNode( "不在线的好友 ");
treemode.insertNodeInto(online   ,   root,0);
treemode.insertNodeInto(notonline   ,root,1);
tree.setRowHeight(20);
tree.scrollPathToVisible(new   TreePath(notonline.getPath()));  
tree.addMouseListener(new   MouseListenerimp());
return   tree;
}

public   JTree   onlinefriend(User   user,int   id)
{
treemode=(DefaultTreeModel)tree.getModel();//得到DefaultTreeModel
for(int   i=0;i <user.getFriend().size();i++)
{
user=(User)user.getFriend().elementAt(0);
if(user.getStatus()==0)

{
System.out.println( "tree "+user.getID());//好于的ID
DefaultMutableTreeNode   newNode=new   DefaultMutableTreeNode(String.valueOf(user.getID()));
treemode.insertNodeInto(newNode   ,   online,   i);
tree.scrollPathToVisible(new   TreePath(newNode.getPath()));    
tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);//只能选取一个节点
tree.addMouseListener(new   MouseListenerimp());
System.out.println( "dsfasfa ");

}
}

return   tree;
}
public   void   addonlinefriend(int   id)
{

treemode=(DefaultTreeModel)tree.getModel();//得到DefaultTreeModel
DefaultMutableTreeNode   newNode=new   DefaultMutableTreeNode(String.valueOf(id));

treemode.insertNodeInto(newNode   ,   online,   0);
tree.setRowHeight(20);

tree.scrollPathToVisible(new   TreePath(newNode.getPath()));    

tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);//只能选取一个节点
tree.setRowHeight(20);

}
public   void   addnotonlinefriend(int   id)
{

treemode=(DefaultTreeModel)tree.getModel();//得到DefaultTreeModel
DefaultMutableTreeNode   newNode=new   DefaultMutableTreeNode(String.valueOf(id));

treemode.insertNodeInto(newNode   ,   notonline,   notonline.getChildCount());

tree.scrollPathToVisible(new   TreePath(newNode.getPath()));    

tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);//只能选取一个节点

tree.setRowHeight(30);


}
public   void   liveline(User   user,int   id)
{

delNode(user,id);//首先在在线的好友节点中   删除离线的那位好友
addNode(id);//再在不在线的好友节点中   添加离线的那位好友

}
public   void   shangxian(User   user,int   id)


{
delNode2(user,id);//首先在不在线的好友节点中   删除离线的那位好友
addNode2(id);//再在在线的好友节点中   添加离线的那位好友



}
public   void   delNode2(User   user,int   id)//删除节点
{
DefaultMutableTreeNode   n=new   DefaultMutableTreeNode();
if   (notonline.getChildCount()   >   0)   {//若在线好友大于零
for   (Enumeration   e=notonline.children();   e.hasMoreElements();   )   {
n   =   (   DefaultMutableTreeNode)e.nextElement();
System.out.println( "将在好友在不在线列表中删除此好友 "+n.toString());
if(n.toString().equals(String.valueOf(id)))
{
notonline.remove(n);//查找在线的列表中有没有此ID有则删除
treemode.reload();


}
}
}
}

public   void   delNode(User   user,int   id)//删除节点
{
DefaultMutableTreeNode   n=new   DefaultMutableTreeNode();
if   (online.getChildCount()   >   0)   {//若在线好友大于零
for   (Enumeration   e=online.children();   e.hasMoreElements();   )   {
n   =   (   DefaultMutableTreeNode)e.nextElement();
System.out.println( "将在在线好友列表中删除此好友 "+n.toString());
if(n.toString().equals(String.valueOf(id)))
{
online.remove(n);//查找在线的列表中有没有此ID有则删除
treemode.reload();
tree.scrollPathToVisible(new   TreePath(n.getPath()));
}
}
}

}
public   void   addNode(int   id)
{
treemode=(DefaultTreeModel)tree.getModel();//得到DefaultTreeModel
DefaultMutableTreeNode   newNode=new   DefaultMutableTreeNode(String.valueOf(id));
treemode.insertNodeInto(newNode   ,   notonline,   notonline.getChildCount());
tree.scrollPathToVisible(new   TreePath(newNode.getPath()));  

}
public   void   addNode2(int   id)
{
treemode=(DefaultTreeModel)tree.getModel();//得到DefaultTreeModel
DefaultMutableTreeNode   newNode=new   DefaultMutableTreeNode(String.valueOf(id));
treemode.insertNodeInto(newNode   ,   online,   online.getChildCount());
tree.scrollPathToVisible(new   TreePath(newNode.getPath()));  

}

public   void   valueChanged(TreeSelectionEvent   e)   {

JTree   tree   =   (JTree)   e.getSource();  
DefaultMutableTreeNode   parentNode=null;
if(tree.getSelectionPath()==null)
{

}else
{

TreePath   parentPath=tree.getSelectionPath();//得到选取到的节点的名字
parentNode=(DefaultMutableTreeNode)(parentPath.getLastPathComponent());

nodenameAll=parentNode.toString();
if(nodenameAll== "我的好友 "||nodenameAll== "在线的好友 "||nodenameAll== "不在线的好友 ")
{
System.out.println( "选择了错误的节点 ");

}else
{

}
}
}
class   MouseListenerimp   extends   MouseAdapter
{

public   void   mousePressed(MouseEvent   e)   {
int   selRow   =   tree.getRowForLocation(e.getX(),   e.getY());


                TreePath   selPath   =   tree.getPathForLocation(e.getX(),   e.getY());
                System.out.println( "选择了第几行 "+selRow);
                DefaultMutableTreeNode   selct=(DefaultMutableTreeNode)selPath.getLastPathComponent();


              //   selct
             
if(e.getClickCount()==2)
{
System.out.println(selct.toString()+ "nodename ");
client.openChatUI(selct.toString());
}
this.mouseReleased(e);

}
//   TODO   自动生成方法存根

public   void   mouseReleased(MouseEvent   e)   {
//   TODO   自动生成方法存根
if(e.isPopupTrigger())
{
System.out.println(e.getComponent().toString());

//得到被右键点击的的节点
PopupMenu   popupMenu1   =   new   PopupMenu();  
MenuItem   menuItem1   =   new   MenuItem();  
MenuItem   menuItem2   =   new   MenuItem();  
MenuItem   menuItem3   =   new   MenuItem();  
menuItem1.setLabel( "删除好友 ");  
menuItem2.setLabel( "查看信息 ");  
menuItem3.setLabel( "聊天 ");  
popupMenu1.add(menuItem1);  
popupMenu1.add(menuItem2);  
popupMenu1.add(menuItem3);  
System.out.println( "e.getc "+e.getComponent()+ "孩子 "+tree.getModel().getChildCount(online));
popupMenu1.show(e.getComponent(),e.getX(),e.getY());   ;    


}


}


}


public   String   getNodenameAll()   {
return   nodenameAll;
}

public   void   setNodenameAll(String   nodenameAll)   {
this.nodenameAll   =   nodenameAll;
}
public   DefaultMutableTreeNode   getNotonline()   {
return   notonline;
}
public   void   setNotonline(DefaultMutableTreeNode   notonline)   {
this.notonline   =   notonline;
}
public   DefaultMutableTreeNode   getOnline()   {
return   online;
}
public   void   setOnline(DefaultMutableTreeNode   online)   {
this.online   =   online;
}


}

[解决办法]
我调试了一下,PopupMenu popupMenu1 = new PopupMenu();
MenuItem menuItem1 = new MenuItem();
MenuItem menuItem2 = new MenuItem();
MenuItem menuItem3 = new MenuItem();
menuItem1.setLabel( "删除好友 ");
menuItem2.setLabel( "查看信息 ");
menuItem3.setLabel( "聊天 ");
popupMenu1.add(menuItem1);
popupMenu1.add(menuItem2);
popupMenu1.add(menuItem3);
将PopupMenu 改为JPopupMenu, MenuItem 改为JMenuItem ,就对了,可能是awt的包中的内与swing包中的内不匹配吧,具体原因我也不太清楚.
[解决办法]
楼主,送分吧^o^

popupMenu1.show(e.getComponent(),e.getX(),e.getY());
调用这段代码之前必须保证e.getComponent()的组件必须已经add了这个popupMenu1,否则会出现parent is null这个异常。

根据你的代码我觉得有两种解决方案:
1、换成JPopupMenu和JMenuItem,据我测试,swing不存在这个问题;
2、将你的MouseListenerimp构造函数改一下,加上一个参数Component parent,用来传递调用者,然后在事件里生成PopupMenu的时候记得先调用parent.add(popupMenu1);就可以了。

我原来遇到过这种问题,但是不知道原因,只知道解决方法,希望有高手能指点一下^o^


[解决办法]
tree.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
int n = tree.getRowForLocation(e.getX(), e.getY());
if (n < 0)
return;
if (e.getButton() == MouseEvent.BUTTON3) {
tree.setSelectionRow(n);
m_popmenu.show(tree, e.getX(), e.getY());


}
}
});

热点排行