感觉这样加是不行的吧,数据操作肯定要再treemodel里面吧

解决方案 »

  1.   

    这不是对数据进行操作,是对树中的节点进行选取并显示,应该只用TreeSelectionModel,使用其的addSelectionPath()方法设置选取,请各位指教!
      

  2.   

    顺便给出提示,呵呵
        DefaultTreeModel model = jTree1.getModel();
        model.insertNodeInto();
    //insertNodeInto(MutableTreeNode newChild, MutableTreeNode parent, int index)
      

  3.   

    我的代码如下:TreePath[] tps = jTree1.getSelectionPaths();
        TreeSelectionModel sm = jTree1.getSelectionModel();
        sm.addSelectionPath(new TreePath(new String("JTree, food, pizza")));是对已存在的节点的选取啊!
      

  4.   

    分特,原来你说这个,我知道你为什么不能选中了
    TreePath[] tps = jTree1.getSelectionPaths();
    TreeSelectionModel sm = jTree1.getSelectionModel();
    sm.addSelectionPath(new TreePath(new String("JTree, food, pizza")));
                         ~~~~~~~~~~~~~`~~~~~~~~~~~~~~~~~~~~~~~~~~`
    这是新的treepath,不是已经有的treepath,所以你设置
    选中的东西部可能在树上显示出来,明白不?
    呵呵。收分罗
      

  5.   

    这样试试:
        tree.setSelectionPath(path);
    path 为输入路径
      

  6.   

    DefaultTreeCellRenderer cellRenderer = (DefaultTreeCellRenderer)jTree1.getCellRenderer();cellRenderer.setFont(new Font("宋体",Font.PLAIN,12));
             cellRenderer.setBackgroundNonSelectionColor(Color.white);
             cellRenderer.setBackgroundSelectionColor(Color.yellow);
             cellRenderer.setBorderSelectionColor(Color.red);
             cellRenderer.setTextNonSelectionColor(Color.black);
             cellRenderer.setTextSelectionColor(Color.blue);