大家好,下面的程序目的是将根结点到指定的结点的路径展开
不知道为什么没有展开,希望能得到大家的帮助
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.tree.*;
import java.util.*;public class MyFrame extends JFrame {
  JPanel contentPane;
  JScrollPane jScrollPane1 = new JScrollPane();
  JTree jTree1 = new JTree();
  JButton jButton1 = new JButton();  //Construct the frame
  public MyFrame() {
    enableEvents(AWTEvent.WINDOW_EVENT_MASK);
    try {
      jbInit();
    }
    catch(Exception e) {
      e.printStackTrace();
    }
  }  //Component initialization
  private void jbInit() throws Exception  {
    contentPane = (JPanel) this.getContentPane();
    contentPane.setLayout(null);
    this.setSize(new Dimension(507, 341));
    this.setTitle("Frame Title");
    jScrollPane1.setBounds(new Rectangle(78, 45, 349, 203));
    jButton1.setBounds(new Rectangle(175, 261, 122, 31));
    jButton1.setText("jButton1");
    jButton1.addActionListener(new MyFrame_jButton1_actionAdapter(this));
    contentPane.add(jScrollPane1, null);
    contentPane.add(jButton1, null);
    jScrollPane1.getViewport().add(jTree1, null);    //TreePath path = findByName(jTree1, new String[]{"JTree", "food", "bananas"});    //jTree1.expandRow(3);  }  //Overridden so we can exit when window is closed
  protected void processWindowEvent(WindowEvent e) {
    super.processWindowEvent(e);
    if (e.getID() == WindowEvent.WINDOW_CLOSING) {
      System.exit(0);
    }
  }
//************************************************************
   // Create tree
// Search forward from first visible row looking for any visible node
// whose name starts with prefix.
int startRow = 0;
String prefix = "b";
public TreePath find(JTree tree, Object[] nodes) {
    TreeNode root = (TreeNode)tree.getModel().getRoot();
    return find2(tree, new TreePath(root), nodes, 0, false);
}// Finds the path in tree as specified by the array of names.
// The names array is a sequence of names where names[0]
// is the root and names[i] is a child of names[i-1].
// Comparison is done using String.equals().
// Returns null if not found.
public TreePath findByName(JTree tree, String[] names) {
    TreeNode root = (TreeNode)tree.getModel().getRoot();
    return find2(tree, new TreePath(root), names, 0, true);
}
private TreePath find2(JTree tree, TreePath parent, Object[] nodes, int depth, boolean byName) {
    TreeNode node = (TreeNode)parent.getLastPathComponent();
    Object o = node;    // If by name, convert node to a string
    if (byName) {
        o = o.toString();
    }
    // If equal, go down the branch
    if (o.equals(nodes[depth])) {
        // If at end, return match
        if (depth == nodes.length-1) {
            return parent;
        }        // Traverse children
        if (node.getChildCount() >= 0) {
            for (Enumeration e=node.children(); e.hasMoreElements(); ) {
                TreeNode n = (TreeNode)e.nextElement();
                TreePath path = parent.pathByAddingChild(n);
                TreePath result = find2(tree, path, nodes, depth+1, byName);
                // Found a match
                if (result != null) {
                    return result;
                }
            }
        }
    }
    // No match at this branch
    return null;
}//************************************************************
  void jButton1_actionPerformed(ActionEvent e) {     TreePath path = findByName(jTree1, new String[]{"JTree", "food", "bananas"});
     jTree1.expandPath(path);
     System.out.println(path.toString());      // jTree1.expandPath(path);
  }
    public static void main(String[] args) {
      new MyFrame();
  }
}class MyFrame_jButton1_actionAdapter implements java.awt.event.ActionListener {
  MyFrame adaptee;  MyFrame_jButton1_actionAdapter(MyFrame adaptee) {
    this.adaptee = adaptee;
  }
  public void actionPerformed(ActionEvent e) {
    adaptee.jButton1_actionPerformed(e);
  }
}

解决方案 »

  1.   

    工程文件内容如下:
    package ontoedit;import javax.swing.UIManager;
    import java.awt.*;
    public class MyApp {
      
    boolean packFrame = false;  //Construct the application
      public MyApp() {
        MyFrame frame = new MyFrame();
        //Validate frames that have preset sizes
        //Pack frames that have useful preferred size info, e.g. from their layout
        if (packFrame) {
          frame.pack();
        }
        else {
          frame.validate();
        }
        //Center the window
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        Dimension frameSize = frame.getSize();
        if (frameSize.height > screenSize.height) {
          frameSize.height = screenSize.height;
        }
        if (frameSize.width > screenSize.width) {
          frameSize.width = screenSize.width;
        }
        frame.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
        frame.setVisible(true);
      }  //Main method
      public static void main(String[] args) {
        try {
          UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        }
        catch(Exception e) {
          e.printStackTrace();
        }
        new MyApp();
      }
    }
      

  2.   

    程序太长了!看了累,建议楼主看一下sun 的java demo!
    C:\j2sdk1.4.2_05\demo\jfc\SampleTree\src
    很不错的例子,排除解决你问题的原因,估计楼主能够学到更多!
      

  3.   

    public void expandPath(TreePath path)
    Ensures that the node identified by the specified path is expanded and viewable. If the last item in the path is a leaf, this will have no effect. path的最后一个节点如果是叶子节点的话,这个方法就不起作用了
    可以把  jTree1.expandPath(path); 改成  jTree1.expandPath(path.getParentPath());
    或者改成 jTree1.setSelectionPath(path); 选中这个path