swing鼠标悬停_Java Swing - 如何将鼠标光标悬停在JTree中的节点上...

import java.util.Enumeration;

import javax.swing.JFrame;

import javax.swing.JTree;

import javax.swing.tree.DefaultMutableTreeNode;

import javax.swing.tree.DefaultTreeModel;

public class Main {

public Main() {

DefaultMutableTreeNode root = new DefaultMutableTreeNode("ROOT");

DefaultTreeModel model = new DefaultTreeModel(root);

JTree tree = new JTree(model);

buildTreeFromString(model, "A/D/E/Node 4");

buildTreeFromString(model, "A/C/F/Node 5");

buildTreeFromString(model, "A/B/G/Node 6");

buildTreeFromString(model, "A/C/H/Node 5");

buildTreeFromString(model, "A/B/G/Node 5");

JFrame f = new JFrame();

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

f.add(tree);

f.setSize(300, 300);

f.setLocation(200, 200);

f.setVisible(true);

}

private void buildTreeFromString(final DefaultTreeModel model,

final String str) {

DefaultMutableTreeNode root = (DefaultMutableTreeNode) model.getRoot();

String[] strings = str.split("/");

DefaultMutableTreeNode node = root;

for (String s : strings) {

int index = childIndex(node, s);

if (index < 0) {

DefaultMutableTreeNode newChild = new DefaultMutableTreeNode(s);

node.insert(newChild, node.getChildCount());

node = newChild;

} else {

node = (DefaultMutableTreeNode) node.getChildAt(index);

}

}

}

private int childIndex(final DefaultMutableTreeNode node,

final String childValue) {

Enumeration children = node.children();

DefaultMutableTreeNode child = null;

int index = -1;

while (children.hasMoreElements() && index < 0) {

child = children.nextElement();

if (child.getUserObject() != null

&& childValue.equals(child.getUserObject())) {

index = node.getIndex(child);

}

}

return index;

}

public static void main(String[] args) {

new Main();

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值