4

I have a classic JTree populated with some nods. Lets assume tree looks like this:

Root
|-Fruit
|--Apple
|--Orange
|-Objects
|--Table
|--Car

Is there any way in java to check if node exists using assumed path like this:

TreeNode found = model.getNodeOrNull("\\Fruit\\Apple")

so if node in given location exists it's returned, if not null is returned? Is there any such mechanism in Java?

mKorbel
  • 109,525
  • 20
  • 134
  • 319
guest86
  • 2,894
  • 8
  • 49
  • 72
  • Are you looking to find a node that matches "Apple" using its toString representation? You can have two nodes that are not equal() but have the same toString(), so your method may return multiple matches. – sjr Jun 04 '12 at 15:13
  • @guest86 no idea what do you trying, you have to iterating in XxxTreeModel, there no another way – mKorbel Jun 05 '12 at 08:39

3 Answers3

5

You might experiment with something along these lines.

Tree Node Location

Example Output

food:pizza found true
od:pizza found false
sports:hockey found true
sports:hockey2 found false

TreeNodeLocation.java

import java.awt.BorderLayout;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.Position;
import javax.swing.tree.TreePath;

public class TreeNodeLocation {

    private JTree tree = new JTree();

    TreeNodeLocation() {
        JPanel p = new JPanel(new BorderLayout(2,2));

        final JTextField find = new JTextField("food:pizza");
        find.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent ae) {
                boolean found = findText(find.getText());
                System.out.println(find.getText() + " found " + found);
            }
        });
        p.add(find, BorderLayout.PAGE_START);

        tree.setVisibleRowCount(8);
        for (int row=tree.getRowCount(); row>=0; row--) {
            tree.expandRow(row);
        }

        p.add(new JScrollPane(tree),BorderLayout.CENTER);

        JOptionPane.showMessageDialog(null, p);
    }

    public boolean findText(String nodes) {
        String[] parts = nodes.split(":");
        TreePath path = null;
        for (String part : parts) {
            int row = (path==null ? 0 : tree.getRowForPath(path));
            path = tree.getNextMatch(part, row, Position.Bias.Forward);
            if (path==null) {
                return false;
            }
        }
        tree.scrollPathToVisible(path);
        tree.setSelectionPath(path);

        return path!=null;
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new TreeNodeLocation();
            }
        });
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
2

You can search using one of model's Enumeration instances, as shown here.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
2

Unfortunately, no there is nothing out of the box that does that. That makes some assumptions about nodes that Swing didn't want to impose of their design because it would constrain what it means to be a node in a tree (namely all nodes have some sort of string that uniquely identifies them). That doesn't mean you can't easily implement it yourself. You maybe aware TreePath is a common idea in JTree so here is a simple method that returns a TreePath to the node given a path:

http://www.exampledepot.8waytrips.com/egs/javax.swing.tree/FindNode.html

You could use that as a basis to implement what you are after.

gumption
  • 1,108
  • 13
  • 10
chubbsondubs
  • 37,646
  • 24
  • 106
  • 138