程序必须实现的功能:
树中没有子节点,只有根节点,通过在树的节点上点击鼠标右键,弹出popupmenu;点击菜单上的菜单项,就可以实现对树的操作--包括建立当前节点的子节点,删除当前节点等等。
本程序的目的是工作流建模的一部分。实现的是对DAO对象的生成,注册。因为DAO对象中的SQL语句可能含有从表,使用树能比较直观。
代码如下:
//功能描述:
//在一个滑动窗体中的树,通过点击其节点(叶子节点或者非叶子节点),出现一个弹出菜单;
//通过电击弹出菜单的菜单项(插入,删除),可以在该节点上实现新节点的插入或者老节点的删除
//在
//弹出菜单是通过鼠标点击产生,并不依附在某个组件上
//能不能控制事件,在鼠标点击树节点的事件转化为一般的鼠标点击事件,并在该事件下产生弹出菜单?
import javax.swing.JPanel;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import java.awt.BorderLayout;
import javax.swing.JScrollPane;
import javax.swing.JFrame;
import javax.swing.event.TreeSelectionListener;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.tree.TreeSelectionModel;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
import java.awt.Component;
import java.awt.event.MouseAdapter;
import javax.swing.SwingUtilities;
import javax.swing.tree.TreePath;
//先把tree放在JScrollPane中,再把JScrollPane放在JPanel中,最后放在顶层容器中
public class Tree
extends JPanel {
private JTree tree;
private DefaultMutableTreeNode root;
private DefaultTreeModel treeModel;
private JScrollPane middlepane;
private JPopupMenu popupmenu;
public Tree() {
setLayout(new BorderLayout());
// createPopupmenu();
// JPanel panel = new JPanel();
// panel.add(popupmenu);
// panel.addMouseListener(this);
root = new DefaultMutableTreeNode("root");
tree = new JTree(root);
tree.getSelectionModel().setSelectionMode(TreeSelectionModel.
SINGLE_TREE_SELECTION);
treeModel = (DefaultTreeModel) tree.getModel();
popupmenu = new JPopupMenu();
JMenuItem item=new JMenuItem("insert");
popupmenu.add(item);
item=new JMenuItem("remove");
popupmenu.add(item);
tree.add(popupmenu);
tree.addMouseListener(new MouseAdapter() {
public void mouseReleased(MouseEvent e) {
try {
if (e.getClickCount() == 1 && SwingUtilities.isRightMouseButton(e)) { //SwingUtilities
TreePath path = tree.getPathForLocation(e.getX(), e.getY());
if (path == null) {
return;
}
tree.setSelectionPath(path);
popupmenu.show(tree, e.getX(), e.getY());
}
else if (e.getClickCount() == 1) {
TreePath path = tree.getPathForLocation(e.getX(), e.getY());
if (path == null) {
return;
}
if (tree.isExpanded(path)) {
tree.collapsePath(path);
}
tree.expandPath(path);
}
return;
}
catch (Exception ex) {
ex.printStackTrace();
}
}
});
JScrollPane middlepane = new JScrollPane(tree);
// middlepane.add(tree);
// middlepane.add(panel);
add(middlepane);
//能不能在装载树的面板上装载弹出菜单?
// add(new JScrollPane(tree));
tree.addTreeSelectionListener(new TreeSelectionListener() {
public void valueChanged(TreeSelectionEvent e) {
DefaultMutableTreeNode selected = (DefaultMutableTreeNode)
tree.getLastSelectedPathComponent();
// e.get
myPopupmenu popup = new myPopupmenu(selected); //在自定义菜单中插入或者删除节点
// selected.
popup.show(null, 0, 0); //popup是一个自定义的菜单,通过点击其菜单项对特定的节点(selected)
//实现相应的操作
//popup.show()的功能是在特定的节点处显示弹出 菜单
}
});
}
public static void main(String arg[]) {
JFrame jf = new JFrame();
jf.getContentPane().add(new Tree(), BorderLayout.CENTER);
jf.setSize(500, 500);
jf.pack();
jf.setVisible(true);
}
class myPopupmenu
extends JPanel
implements MouseListener {
private DefaultMutableTreeNode selectnode;
private JPopupMenu popupmenu;
private JMenuItem item;
public myPopupmenu(DefaultMutableTreeNode e) {
setLayout(new BorderLayout());
this.selectnode = e;
this.popupmenu = new JPopupMenu();
item = new JMenuItem("insert");
// item.addMouseListener(new myMouseListener());
this.popupmenu.add(item);
item = new JMenuItem("remove");
// item.addMouseListener(new myMouseListener());
this.popupmenu.add(item);
JPanel panel = new JPanel();
panel.add(popupmenu);
// panel.p
add(panel);
panel.addMouseListener(this);
// pack();
}
public void show(Component com, int x, int y) {
// this.popupmenu.show(com,x,y);
}
/*
public static void main(String arg[]) {
JFrame jf = new JFrame();
jf.getContentPane().add(new myPopupmenu(null), BorderLayout.CENTER);
jf.setSize(200, 200);
jf.pack();
jf.setVisible(true);
}
*/
public void mouseClicked(MouseEvent mouseEvent) {
}
public void mousePressed(MouseEvent mouseEvent) {
if (mouseEvent.getButton() == 1) {
popupmenu.show(this, mouseEvent.getX(), mouseEvent.getY());
}
}
public void mouseReleased(MouseEvent mouseEvent) {
}
public void mouseEntered(MouseEvent mouseEvent) {
}
public void mouseExited(MouseEvent mouseEvent) {
}
}
}