给JTree节点添加右键刷新菜单

不多bb直接贴代码和效果

package swing;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
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;
import javax.swing.JSplitPane;
import javax.swing.JTree;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreePath;

public class MyTreePanel extends JPanel {
	private JTree tree;
	private DefaultTreeModel treeModel;
	private JSplitPane mainSplitPane;
	private JPanel leftTreePanel;
	private JPanel rightCurrentPanel;
	public MyTreePanel() {
		// TODO Auto-generated constructor stub
		setLayout(new BorderLayout());
		mainSplitPane=new JSplitPane();
		mainSplitPane.setDividerLocation(0.2);
		
		leftTreePanel=new JPanel();
		rightCurrentPanel=new JPanel();
		
		initTreeData();
	}

	private void initTreeData() {
		// TODO Auto-generated method stub
		DefaultMutableTreeNode root=new DefaultMutableTreeNode("星宿");
		DefaultMutableTreeNode node_1=new DefaultMutableTreeNode("天干");
		DefaultMutableTreeNode node_2=new DefaultMutableTreeNode("地支");
		root.add(node_1);
		root.add(node_2);
		treeModel=new DefaultTreeModel(root);
		tree=new JTree();
		tree.setModel(treeModel);
		
		JPopupMenu menu=new JPopupMenu();		//创建菜单
		JMenuItem menuItem=new JMenuItem("刷新F5");//创建菜单项(点击菜单项相当于点击一个按钮)
		menu.add(menuItem);
		//菜单项绑定监听
		menuItem.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				System.out.println("假装刷新一下子吧");
				//dosomething
			}
		});
		
		tree.addTreeSelectionListener(new TreeSelectionListener() {
			
			@Override
			public void valueChanged(TreeSelectionEvent e) {
				// TODO Auto-generated method stub
				DefaultMutableTreeNode sel_node = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();
				if(null!=sel_node){
					Object userObject = sel_node.getUserObject();
					if(null!=userObject){
						if(userObject instanceof String){
							String objectName = userObject.toString();
							switch(objectName){
							case "天干":
								System.out.println("天干");
								rightCurrentPanel=new TianGanPanel();
								mainSplitPane.add(rightCurrentPanel,JSplitPane.RIGHT);
								
								break;
							case "地支":
								System.out.println("地支");
								//rightCurrentPanel=new DiZhiPanel();
								break;
							default:
								break;
							}
						}
					}
				}
			}
		});
		
		tree.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				// TODO Auto-generated method stub
				super.mouseClicked(e);
				int x = e.getX();
				int y = e.getY();
				
				if(e.getButton()==MouseEvent.BUTTON3){
					//menuItem.doClick(); //编程方式点击菜单项
					TreePath pathForLocation = tree.getPathForLocation(x, y);//获取右键点击所在树节点路径
					
					tree.setSelectionPath(pathForLocation);
					menu.show(tree, x, y);
					
				}
			}
		});
		
		leftTreePanel.add(tree);
		mainSplitPane.add(leftTreePanel, JSplitPane.LEFT);
		mainSplitPane.add(rightCurrentPanel, JSplitPane.RIGHT);
		add(mainSplitPane,BorderLayout.CENTER);
	}
	
	public static void main(String[] args) {
		MyTreePanel myTreePanel = new MyTreePanel();
		JFrame f=new JFrame("树测试");
		f.setSize(1000, 600);
		f.add(myTreePanel,BorderLayout.CENTER);
		f.setVisible(true);
		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
}

以下是效果

给JTree节点添加右键刷新菜单_第1张图片

如果觉得有帮助就给我点个赞哦

你可能感兴趣的:(给JTree节点添加右键刷新菜单)