在java中如何响应单击JTree树中一个节点?

2024年11月15日 10:28
有2个网友回答
网友(1):

我给你写了一个例子 你看看吧 你看了就明白了

package tree;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.Event;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.beans.beancontext.BeanContext;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.JTree;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;

import com.sun.xml.internal.messaging.saaj.packaging.mime.util.BEncoderStream;

public class MainFrame extends JFrame {

public static void main(String args[]) {
try {
MainFrame frame = new MainFrame();
Dimension frameSize = frame.getSize();
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
if(frameSize.width>screenSize.width){
frameSize.width=screenSize.width;
}
if(frameSize.height>screenSize.height){
frameSize.height=screenSize.height;
}

frame.setLocation((screenSize.width-frameSize.width)/2, (screenSize.height-frameSize.height)/2);

frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}

private JTree tree;
private DefaultMutableTreeNode root = new DefaultMutableTreeNode("语言");
private DefaultMutableTreeNode menu_accp = new DefaultMutableTreeNode("英语");
private DefaultMutableTreeNode menu_benet = new DefaultMutableTreeNode("法语");
private DefaultMutableTreeNode menu_accp_s1 = new DefaultMutableTreeNode("4级");
private DefaultMutableTreeNode menu_accp_s2 = new DefaultMutableTreeNode("6级");
private DefaultMutableTreeNode menu_accp_y2 = new DefaultMutableTreeNode("8级");

private JTextField txtParent;
private JTextField txtNode;
private JTextField txtType;
private JTextField txtChild;
private JPopupMenu popupMenu;

/**
* Create the frame
*/
public MainFrame() {
super();
getContentPane().setLayout(null);
setBounds(100, 100, 535, 375);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

final JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(24, 10, 173, 317);
getContentPane().add(scrollPane);
tree = new JTree(root);
tree.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if(e.getModifiers()==Event.META_MASK){
addPopup(getContentPane(), popupMenu);
popupMenu.show(getContentPane(),e.getX()+30,e.getY()+40);
}
}
});
tree.addTreeSelectionListener(new TreeSelectionListener() {
public void valueChanged(TreeSelectionEvent e) {
DefaultMutableTreeNode node = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();
Object nodeInfo = node.getUserObject();

if(node.isRoot()){
txtParent.setText("");
txtNode.setText(nodeInfo.toString());
txtType.setText("根节点");
txtChild.setText(node.getChildCount() + "");
}else{
txtParent.setText(node.getParent().toString());
txtNode.setText(nodeInfo.toString());
if(node.isLeaf()){
txtType.setText("叶节点");
}else{
txtType.setText("枝节点");
}
txtChild.setText(node.getChildCount() + "");
}

}
});
scrollPane.setViewportView(tree);

txtParent = new JTextField();
txtParent.setBounds(339, 49, 90, 21);
getContentPane().add(txtParent);

txtNode = new JTextField();
txtNode.setBounds(339, 107, 90, 21);
getContentPane().add(txtNode);

txtType = new JTextField();
txtType.setBounds(339, 167, 90, 21);
getContentPane().add(txtType);

txtChild = new JTextField();
txtChild.setBounds(339, 231, 90, 21);
getContentPane().add(txtChild);

final JLabel label = new JLabel();
label.setText("父类型节点");
label.setBounds(225, 49, 90, 15);
getContentPane().add(label);

final JLabel label_1 = new JLabel();
label_1.setText("节点的名称");
label_1.setBounds(225, 107, 90, 15);
getContentPane().add(label_1);

final JLabel label_2 = new JLabel();
label_2.setText("节点的类型");
label_2.setBounds(225, 167, 90, 15);
getContentPane().add(label_2);

final JLabel label_3 = new JLabel();
label_3.setText("子节点的个数");
label_3.setBounds(225, 231, 90, 15);
getContentPane().add(label_3);

popupMenu = new JPopupMenu();

final JMenuItem menuItem = new JMenuItem();
menuItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
DefaultMutableTreeNode node = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();
DefaultTreeModel model = (DefaultTreeModel) tree.getModel();
model.removeNodeFromParent(node);
}
});
menuItem.setText("删除");
popupMenu.add(menuItem);
//
}

private static void addPopup(Component component, final JPopupMenu popup) {
component.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
if (e.isPopupTrigger())
showMenu(e);
}
public void mouseReleased(MouseEvent e) {
if (e.isPopupTrigger())
showMenu(e);
}
private void showMenu(MouseEvent e) {
popup.show(e.getComponent(), e.getX(), e.getY());
}
});
}
}

网友(2):

xxxTree.addTreeSelectionListener(new TreeSelectionListener() {

@Override
public void valueChanged(TreeSelectionEvent e) {

DefaultMutableTreeNode node = (DefaultMutableTreeNode) e.getPath().getLastPathComponent();
if (node.getUserObject() instanceof User) {
User selectUser= (User) node
.getUserObject();
selectUser(selectUser);// 选择后需要执行的代码
}
}
});