这是源码 需要做的是 右键点击在线好友和不在线的好友的子节点能弹出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;
}
}
有什么问题 马上题
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;
}
}
jTree.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e) {
if (e.getButton() == e.BUTTON3) {
treeNodeMenu.show(jTree, e.getX(), e.getY()); //treeNodeMenu就是定义的弹出工菜单
}
});
用mouseClicked也是一样
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException: parent is null
at java.awt.PopupMenu.show(Unknown Source)
at Client.FriendTree$MouseListenerimp.mouseClicked(FriendTree.java:222)
at java.awt.AWTEventMulticaster.mouseClicked(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
代码较多 我在别的界面中加入了
public Component getComponent()返回事件的始发者。 返回:
发起事件的 Component 对象,如果该对象不是一个 Component,则返回 null。
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包中的内不匹配吧,具体原因我也不太清楚.
调用这段代码之前必须保证e.getComponent()的组件必须已经add了这个popupMenu1,否则会出现parent is null这个异常。根据你的代码我觉得有两种解决方案:
1、换成JPopupMenu和JMenuItem,据我测试,swing不存在这个问题;
2、将你的MouseListenerimp构造函数改一下,加上一个参数Component parent,用来传递调用者,然后在事件里生成PopupMenu的时候记得先调用parent.add(popupMenu1);就可以了。我原来遇到过这种问题,但是不知道原因,只知道解决方法,希望有高手能指点一下^o^
是在调用popupMenu1.show(e.getComponent(),e.getX(),e.getY());之前先调用parent.add(popupMenu1);不好意思....
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;import javax.swing.JFrame;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;public class TestPopupMenu extends JFrame{
private static final long serialVersionUID = 4183586224160506827L; public TestPopupMenu(){
super();
this.getContentPane().add(buildMainPanel());
this.setSize(400,300);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
} private Component buildMainPanel() {
final JPanel panel = new JPanel();
// final PopupMenu menu = new PopupMenu();
// menu.add(new MenuItem("删除好友"));
// menu.add(new MenuItem("查看信息"));
// menu.add(new MenuItem("聊天"));
// panel.add(menu);
final JPopupMenu menu = new JPopupMenu();
menu.add(new JMenuItem("删除好友"));
menu.add(new JMenuItem("查看信息"));
menu.add(new JMenuItem("聊天"));
panel.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e){
if(e.getButton()!=MouseEvent.BUTTON3)
return;
int x = e.getX();
int y = e.getY();
menu.show(panel, x, y);
}
});
return panel;
}
public static void main(String[] args){
new TestPopupMenu();
}
}
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());
}
}
});