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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在java中如何响应单击JTree树中一个节点?相关的知识,希望对你有一定的参考价值。

请诸位帮个忙!
我在java中建立了一个JTree的树,我想完成的功能就是,单击某一个节点后,响应对映的操作。

package myawt;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.tree.*;
import javax.swing.event.*;
public class MyF2 extends JFrame implements TreeSelectionListener
DefaultMutableTreeNode root = new DefaultMutableTreeNode("学校");
DefaultMutableTreeNode root1 = new DefaultMutableTreeNode("教学部");
DefaultMutableTreeNode root2 = new DefaultMutableTreeNode("行政部");
JTree tree1=new JTree(root);
public MyF2()
super("JTree");
this.setSize(800, 600);
root.add(root1);
root.add(root2);

this.add(tree1);
this.setVisible(true);



public static void main(String[] args)
new MyF2();


public void valueChanged(TreeSelectionEvent e)
我只知道要写这样一个方法,但是如何写我不会
例如,单击(或双击)“教学部”这个节点,就会输出“教学部”这几个字。




请大家帮帮忙~!谢谢!

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

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());

);

参考技术A 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);// 选择后需要执行的代码


);

JTree 通过单击行上的任意位置选择节点

【中文标题】JTree 通过单击行上的任意位置选择节点【英文标题】:JTree select node by clicking anywhere on the row 【发布时间】:2012-06-10 00:40:28 【问题描述】:

我有代码taken from here,它允许通过单击行上的任意位置来选择 JTree 行。它在单行选择模式下工作正常。但是,我不确定如何修改它以处理多行选择。如何区分用户进行多项选择时的情况(例如,在一行上单击鼠标左键时按住 shift 或 control 按钮)?

导入 java.awt.Rectangle; 导入 java.awt.event.MouseAdapter; 导入 java.awt.event.MouseEvent; 导入 java.awt.event.MouseListener; 导入 javax.swing.JFrame; 导入 javax.swing.JTree; 导入 javax.swing.SwingUtilities; 导入 javax.swing.tree.DefaultMutableTreeNode; 导入 javax.swing.tree.TreeNode; @SuppressWarnings("串行") 公共类 NavTree 扩展 JTree 私有布尔 fWholeRowSelectionEnabled; 私有 MouseListener fRowSelectionListener; 最终的导航树 fThis; 公共导航树(树节点根节点) 超级(根节点); f这=这; 在里面(); 公共导航树() f这=这; 在里面(); 私人无效初始化() //setCellRenderer(new NavTreeCellRenderer()); fRowSelectionListener = new MouseAdapter() 公共无效鼠标按下(鼠标事件e) if (SwingUtilities.isLeftMouseButton(e)) int 最近行 = fThis.getClosestRowForLocation( e.getX(), e.getY()); 矩形最接近行边界 = fThis.getRowBounds(closestRow); if(e.getY() >= mostRowBounds.getY() && e.getY() nearestRowBounds.getX() && 最接近行

【问题讨论】:

【参考方案1】:

使用MouseEvent 的修饰键信息。更多信息请见MouseEvent#getModifiersEx

【讨论】:

谢谢,但它有点依赖于平台,有没有更好的方法? 它只是依赖于平台,因为它需要一个带有键盘的平台......【参考方案2】:

PS:监听器注册有bug

public void setWholeRowSelectionEnabled(boolean wholeRowSelectionEnabled) 
    fWholeRowSelectionEnabled = wholeRowSelectionEnabled;
    if (fWholeRowSelectionEnabled)
        addMouseListener(fRowSelectionListener);
    else
        removeMouseListener(fRowSelectionListener);

将属性wholeRowSelectionEnabled 设置为true 应该只注册一次监听器。如果属性多次设置为true,您的代码将一次又一次地添加侦听器。我的意思是属性设置器应该是idempotent。

快速修复可能是先删除它,如果启用则添加它

public void setWholeRowSelectionEnabled(boolean wholeRowSelectionEnabled) 
    removeMouseListener(fRowSelectionListener);
    fWholeRowSelectionEnabled = wholeRowSelectionEnabled;
    if (fWholeRowSelectionEnabled)
        addMouseListener(fRowSelectionListener);

【讨论】:

以上是关于在java中如何响应单击JTree树中一个节点?的主要内容,如果未能解决你的问题,请参考以下文章

JTree 通过单击行上的任意位置选择节点

在 JTree 中拖放节点

java中jtree双击叶节点如何获取该文件所在的路径

Java:如何以编程方式选择和扩展 JTree 中的多个节点?

在 Java GUI 中更新 JTree

JAVA 得到jtree 某节点下的子节点