I have a JTree
, and I have a node in the JTree
, but the node is in like 5 to 6 levels deep. And I have a path for the node. I want to expand the JTree
to show the node. Therefore I tried myJTree.expandPath(myJTree.getPath(myNode))
; but it didn't work, it only expanded the first level of myJTree
. What can I do? I have verified the path is valid.
我有一个JTree,我在JTree中有一个节点,但节点的深度为5到6级。我有一个节点的路径。我想扩展JTree以显示节点。因此我尝试了myJTree.expandPath(myJTree.getPath(myNode));但它没有用,它只扩展了myJTree的第一级。我能做什么?我已验证路径有效。
I have the path to the node, and I tried to expand using myJTree.expandPath(myJTree.getPath(myNode))
but it doesn't actually work.
我有节点的路径,我尝试使用myJTree.expandPath(myJTree.getPath(myNode))进行扩展,但它实际上并不起作用。
private TreePath getPath(TreeNode treeNode) {
List<Object> nodes = new ArrayList<Object>();
if (treeNode != null) {
nodes.add(treeNode);
treeNode = treeNode.getParent();
while (treeNode != null) {
nodes.add(0, treeNode);
treeNode = treeNode.getParent();
}
}
return nodes.isEmpty() ? null : new TreePath(nodes.toArray());
}
2 个解决方案
#1
Try this example ... it allows you to search for a node by name and will expand to the first node found
试试这个例子......它允许你按名称搜索节点,并将扩展到找到的第一个节点
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreePath;
public class TreeDemo extends JFrame implements ActionListener{
private static final long serialVersionUID = 1L;
public JTree tree;
public JButton button;
public TreeDemo() {
button = new JButton("Find Queen");
button.addActionListener(this);
tree = new JTree();
DefaultMutableTreeNode root = new DefaultMutableTreeNode( "Deck" );
DefaultMutableTreeNode itemClubs= new DefaultMutableTreeNode( "Clubs" );
addAllCard( itemClubs );
root.add( itemClubs );
DefaultMutableTreeNode itemDiamonds = new DefaultMutableTreeNode( "Diamonds" );
addAllCard( itemDiamonds );
root.add( itemDiamonds );
DefaultMutableTreeNode itemSpades = new DefaultMutableTreeNode( "Spades" );
addAllCard( itemSpades );
root.add( itemSpades );
DefaultMutableTreeNode itemHearts = new DefaultMutableTreeNode( "Hearts" );
addAllCard( itemHearts );
root.add( itemHearts );
DefaultTreeModel treeModel = new DefaultTreeModel( root );
tree = new JTree( treeModel );
JScrollPane scrollPane = new JScrollPane(tree);
getContentPane().add(scrollPane, BorderLayout.CENTER);
getContentPane().add(button, BorderLayout.NORTH);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(375, 400);
}
public void addAllCard( DefaultMutableTreeNode suit ) {
suit.add( new DefaultMutableTreeNode( "Ace" ) );
suit.add( new DefaultMutableTreeNode( "Two" ) );
suit.add( new DefaultMutableTreeNode( "Three" ) );
suit.add( new DefaultMutableTreeNode( "Four" ) );
suit.add( new DefaultMutableTreeNode( "Five" ) );
suit.add( new DefaultMutableTreeNode( "Six" ) );
suit.add( new DefaultMutableTreeNode( "Seven" ) );
suit.add( new DefaultMutableTreeNode( "Eight" ) );
suit.add( new DefaultMutableTreeNode( "Nine" ) );
suit.add( new DefaultMutableTreeNode( "Ten" ) );
suit.add( new DefaultMutableTreeNode( "Jack" ) );
suit.add( new DefaultMutableTreeNode( "Queen" ) );
suit.add( new DefaultMutableTreeNode( "King" ) );
}
public final DefaultMutableTreeNode findNode(String searchString) {
List<DefaultMutableTreeNode> searchNodes = getSearchNodes((DefaultMutableTreeNode)tree.getModel().getRoot());
DefaultMutableTreeNode currentNode = (DefaultMutableTreeNode)tree.getLastSelectedPathComponent();
DefaultMutableTreeNode foundNode = null;
int bookmark = -1;
if( currentNode != null ) {
for(int index = 0; index < searchNodes.size(); index++) {
if( searchNodes.get(index) == currentNode ) {
bookmark = index;
break;
}
}
}
for(int index = bookmark + 1; index < searchNodes.size(); index++) {
if(searchNodes.get(index).toString().toLowerCase().contains(searchString.toLowerCase())) {
foundNode = searchNodes.get(index);
break;
}
}
if( foundNode == null ) {
for(int index = 0; index <= bookmark; index++) {
if(searchNodes.get(index).toString().toLowerCase().contains(searchString.toLowerCase())) {
foundNode = searchNodes.get(index);
break;
}
}
}
return foundNode;
}
private final List<DefaultMutableTreeNode> getSearchNodes(DefaultMutableTreeNode root) {
List<DefaultMutableTreeNode> searchNodes = new ArrayList<DefaultMutableTreeNode>();
Enumeration<?> e = root.preorderEnumeration();
while(e.hasMoreElements()) {
searchNodes.add((DefaultMutableTreeNode)e.nextElement());
}
return searchNodes;
}
public static void main(String[] args) {
TreeDemo app = new TreeDemo();
app.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
DefaultMutableTreeNode node = findNode("Queen");
if( node != null ) {
TreePath path = new TreePath(node.getPath());
tree.setSelectionPath(path);
tree.scrollPathToVisible(path);
}
}
}
#2
What you need is:
你需要的是:
myJTree.expandPath(new TreePath(myNode.getPath()));
#1
Try this example ... it allows you to search for a node by name and will expand to the first node found
试试这个例子......它允许你按名称搜索节点,并将扩展到找到的第一个节点
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreePath;
public class TreeDemo extends JFrame implements ActionListener{
private static final long serialVersionUID = 1L;
public JTree tree;
public JButton button;
public TreeDemo() {
button = new JButton("Find Queen");
button.addActionListener(this);
tree = new JTree();
DefaultMutableTreeNode root = new DefaultMutableTreeNode( "Deck" );
DefaultMutableTreeNode itemClubs= new DefaultMutableTreeNode( "Clubs" );
addAllCard( itemClubs );
root.add( itemClubs );
DefaultMutableTreeNode itemDiamonds = new DefaultMutableTreeNode( "Diamonds" );
addAllCard( itemDiamonds );
root.add( itemDiamonds );
DefaultMutableTreeNode itemSpades = new DefaultMutableTreeNode( "Spades" );
addAllCard( itemSpades );
root.add( itemSpades );
DefaultMutableTreeNode itemHearts = new DefaultMutableTreeNode( "Hearts" );
addAllCard( itemHearts );
root.add( itemHearts );
DefaultTreeModel treeModel = new DefaultTreeModel( root );
tree = new JTree( treeModel );
JScrollPane scrollPane = new JScrollPane(tree);
getContentPane().add(scrollPane, BorderLayout.CENTER);
getContentPane().add(button, BorderLayout.NORTH);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(375, 400);
}
public void addAllCard( DefaultMutableTreeNode suit ) {
suit.add( new DefaultMutableTreeNode( "Ace" ) );
suit.add( new DefaultMutableTreeNode( "Two" ) );
suit.add( new DefaultMutableTreeNode( "Three" ) );
suit.add( new DefaultMutableTreeNode( "Four" ) );
suit.add( new DefaultMutableTreeNode( "Five" ) );
suit.add( new DefaultMutableTreeNode( "Six" ) );
suit.add( new DefaultMutableTreeNode( "Seven" ) );
suit.add( new DefaultMutableTreeNode( "Eight" ) );
suit.add( new DefaultMutableTreeNode( "Nine" ) );
suit.add( new DefaultMutableTreeNode( "Ten" ) );
suit.add( new DefaultMutableTreeNode( "Jack" ) );
suit.add( new DefaultMutableTreeNode( "Queen" ) );
suit.add( new DefaultMutableTreeNode( "King" ) );
}
public final DefaultMutableTreeNode findNode(String searchString) {
List<DefaultMutableTreeNode> searchNodes = getSearchNodes((DefaultMutableTreeNode)tree.getModel().getRoot());
DefaultMutableTreeNode currentNode = (DefaultMutableTreeNode)tree.getLastSelectedPathComponent();
DefaultMutableTreeNode foundNode = null;
int bookmark = -1;
if( currentNode != null ) {
for(int index = 0; index < searchNodes.size(); index++) {
if( searchNodes.get(index) == currentNode ) {
bookmark = index;
break;
}
}
}
for(int index = bookmark + 1; index < searchNodes.size(); index++) {
if(searchNodes.get(index).toString().toLowerCase().contains(searchString.toLowerCase())) {
foundNode = searchNodes.get(index);
break;
}
}
if( foundNode == null ) {
for(int index = 0; index <= bookmark; index++) {
if(searchNodes.get(index).toString().toLowerCase().contains(searchString.toLowerCase())) {
foundNode = searchNodes.get(index);
break;
}
}
}
return foundNode;
}
private final List<DefaultMutableTreeNode> getSearchNodes(DefaultMutableTreeNode root) {
List<DefaultMutableTreeNode> searchNodes = new ArrayList<DefaultMutableTreeNode>();
Enumeration<?> e = root.preorderEnumeration();
while(e.hasMoreElements()) {
searchNodes.add((DefaultMutableTreeNode)e.nextElement());
}
return searchNodes;
}
public static void main(String[] args) {
TreeDemo app = new TreeDemo();
app.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
DefaultMutableTreeNode node = findNode("Queen");
if( node != null ) {
TreePath path = new TreePath(node.getPath());
tree.setSelectionPath(path);
tree.scrollPathToVisible(path);
}
}
}
#2
What you need is:
你需要的是:
myJTree.expandPath(new TreePath(myNode.getPath()));