Sorting of JTree

How to do sorting of JTree child names ?

Thanks WalterLan it is working now.
My next problem is how to sort tree nodes into two sets.
My situation is: I have a JTree with 2 types of members: Primary, Secondary. I want sort tree so that all Primary Members comes into the tree first and then then secondary members. Root Node should be common.
Thank you

Similar Messages

  • Sorting a JTree's childrens

    Please let me know how to sort a JTree (Sorting in alphabetical order).
    Thanks.

    Wen you insert node, insert it at correct place.
    Store a Vector of all children in the node write a , write an Utility insert method, which does the binary search on the Vector and insertElementAt(positionResultOfBinarySearch).

  • Sort JTree alphabetical

    Hello everyone! I am trying to sort a JTree alphabetical. For the moment i made only the roots (partitions) to sort. I use the JTree to view all the partitions and view all the files. I override the interface TreeNode for my needs...and i made a class for cell renderer...
    public class FileTreeNode implements TreeNode
         public File file;
         private File[] children;     
         private  TreeNode parent;     
         public boolean isFileSystemRoot;     
         public FileTreeNode(File file, boolean isFileSystemRoot, TreeNode parent)
              this.file = file;
              this.isFileSystemRoot = isFileSystemRoot;
              this.parent = parent;
              this.children = this.file.listFiles();
              if (this.children == null)
                   this.children = new File[0];
         public FileTreeNode(File[] children)
              this.file = null;
              this.parent = null;
              this.children = children;               
         @Override
         public Enumeration<?> children()
              final int elementCount = this.children.length;
              return new Enumeration<File>()
                   int count = 0;
                   public boolean hasMoreElements()
                        return this.count < elementCount;
                   public File nextElement()
                        if (this.count < elementCount)
                             return FileTreeNode.this.children[this.count++];
                        throw new NoSuchElementException("Vector Enumeration");
         @Override
         public boolean getAllowsChildren()
              return true;
         @Override
         public TreeNode getChildAt(int childIndex)
              return new FileTreeNode(this.children[childIndex],this.parent == null, this);
         @Override
         public int getChildCount()
              return this.children.length;
         @Override
         public int getIndex(TreeNode node)
              FileTreeNode ftn = (FileTreeNode) node;
              for (int i = 0; i < this.children.length; i++)
                   if (ftn.file.equals(this.children))
                        return i;                    
              return -1;
         @Override
         public TreeNode getParent()
              return this.parent;
         @Override
         public boolean isLeaf()
              return (this.getChildCount() == 0);
         public void sortRoots()
              List<File> list = Arrays.asList(this.children);
              Collections.sort(list);
              System.out.println("Partitions: " + list);
    and the other class....public class FileTreeCellRenderer extends DefaultTreeCellRenderer
         private static final long serialVersionUID = 1L;
         protected static FileSystemView fsv = FileSystemView.getFileSystemView();
         private Map<String, Icon> iconCache = new HashMap<String, Icon>();          
         private Map<File, String> rootNameCache = new HashMap<File, String>();     
         public Component getTreeCellRendererComponent(JTree tree, Object value,
                   boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus)
              FileTreeNode ftn = (FileTreeNode) value;
              File file = ftn.file;
              String filename = "";
              if (file != null)
                   if (ftn.isFileSystemRoot)
                        filename = this.rootNameCache.get(file);
                        if (filename == null)
                             filename = fsv.getSystemDisplayName(file);                         
                             this.rootNameCache.put(file, filename);
                   else
                        filename = file.getName();
              JLabel result = (JLabel) super.getTreeCellRendererComponent(tree,
                        filename, selected, expanded, leaf, row, hasFocus);
              if (file != null)
                   Icon icon = this.iconCache.get(filename);
                   if (icon == null)
                        icon = fsv.getSystemIcon(file);
                        this.iconCache.put(filename, icon);
                   result.setIcon(icon);
              return result;
    and i run the application...File[] roots = File.listRoots();
    FileTreeNode rootTreeNode = new FileTreeNode(roots);
    tree = new JTree(rootTreeNode);
    tree.setCellRenderer(new FileTreeCellRenderer());
    can anyone please help me to sort alphabetical all the files from all the nodes...any help will be appreciated.
    Thanks.
    Calin                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       

    might be easier if you were to add a small pictorial
    e.g. if you have a no-arg constructor JTree, you get this
    JTree
          colors
                   blue
                   violet
                   red
                   yellow
          sports
                   basketball
                   soccer
                   football
                   hockey
          food
                   hot dogs
                   pizza
                   ravioli
                   bananasnow put it how you would like it sorted

  • Sorting tree with Sortset/Treeset

    Have anyone tried sorting a jtrees nodes using sortset or treeset? I just want the children for each directory in my tree to be sorted. Collections like treeset and sortset should do the trick, but i have failed in my quest for the right solution.
    Can anyone please help me. I have been searching the forum, and there must be better ways of sorting a jtree than making large functions or subclasses..
    Thank you for helping me.
    Tobbmaister

    I have my own answer.
    When you build a tree, regardles if it's from a database, files og unix-enviroment, this will work.
    Gather all information in collections. For each parent( if you have any, get all children. ) ( this is before you start adding nodes to root.)
    use a while( children.hasNext ) and add one and one element to a TreeSort.
    When all children are in TreeSort, simlply use the getIterator method from TreeSort, and a sorted iterator will be returned.
    Use this iterator and add childrens to parent.
    This worked for me.

  • Sorting the nodes of a JTree

    Can anyone tell me how to go about sorting the nodes in a JTree?

    The best way to do it is thru the treemodel. Either adjust the order in a DefaultTreeModel, or create your own TreeModel.

  • How to sort Jtree node?

    Now, I want to sort tree node by increasing.
    for example there are three child node:
    [20 -40][
    10 - 20]
    [30- 60].
    they are added to JTree node by different order.but are shown in increasingorder.
    like this:
    [10 - 20]
    [20 -40][
    [30- 60].
    how to control tree node.

    Implement the Comparable interface in t he userobject class and possibly the toString(0 as well.
    Ex.
    public class UserObject implements java.lang.Comparable{
    public int compareTo(Object obj) throws ClassCastException
    public String toString()

  • Sorting contents of a JTREE

    Hi all..
    I have a Jtree that needs to be sorted. How can it do it fast and efficiently as the size of the tree can be huge. I have the tree contents that has depth of max 4 or 5. Your help on this is greatly appreciated.
    Thanks
    Srivarun

    Hi there ..
    You can sort the tree by again recreating the tree nodes.
      DefaultMutableTreeNode oldNodes = (DefaultMutableTreeNode)tree.getModel().getRoot();
      DefaultMutableTreeNode newNodes = new DefaultMutableTreeNode(oldNodes.getUserObject());
      void visitNodes(DefaultMutableTreeNode oldNodes,DefaultMutableTreeNode newNodes ){
        Vector sorted = sort(oldNodes.children());
        for (int i = 0; i < sorted.size(); i++)
          DefaultMutableTreeNode chNode = new DefaultMutableTreeNode(sorted.get(i));
          newNodes.add(chNode);
          visitNodes((DefaultMutableTreeNode)sorted.get(i),chNode);
      tree = new Tree(newNodes);hope this would help!
    or you can also checkout the thread .. http://forum.java.sun.com/thread.jsp?forum=57&thread=189640
    Regards,
    Asim

  • Sorting the raws of JTree

    How do i sort the raws which are there in one JTree. But here we have added JTree in JTable. And i want to sort the values of JTree depending on the click of the column of the JTable.

    There is already a topic on this subject
    http://forum.java.sun.com/thread.jsp?forum=57&thread=563889&tstart=45&trange=15
    Denis Krukovsky
    http://dotuseful.sourceforge.net/

  • Sorting JTree problem

    i have created a JTree and have some data in , now i want to sort all data in this JTree, can anyone tell me how to do this? or write a peace of code if you can.

    First give us a chance to know what have you done so far(Coding), post you code first.

  • JTree sorting problem, how to sort

    here is my code:
    DefaultMutableTreeNode root=new DefaultMutableTreeNode("root");
                             DefaultMutableTreeNode childone=new DefaultMutableTreeNode("id");
                             DefaultMutableTreeNode person1=new DefaultMutableTreeNode("david");
                             DefaultMutableTreeNode p11=new DefaultMutableTreeNode("java programming");
                             DefaultMutableTreeNode p12=new DefaultMutableTreeNode("maths");
                             DefaultMutableTreeNode p13=new DefaultMutableTreeNode("c programming");
                             person1.add(p11);
                             person1.add(p12);
                             person1.add(p13);
                             childone.add(person1);
                             DefaultMutableTreeNode person3=new DefaultMutableTreeNode("ann");
                             DefaultMutableTreeNode p31=new DefaultMutableTreeNode("georgian");
                             DefaultMutableTreeNode p32=new DefaultMutableTreeNode("english");
                             DefaultMutableTreeNode p33=new DefaultMutableTreeNode("russian");
                             person3.add(p31);
                             person3.add(p32);
                             person3.add(p33);
                             childone.add(person3);
                             DefaultMutableTreeNode person2=new DefaultMutableTreeNode("merab");
                             DefaultMutableTreeNode p21=new DefaultMutableTreeNode("veterinar");
                             DefaultMutableTreeNode p22=new DefaultMutableTreeNode("building");
                             person2.add(p21);
                             person2.add(p22);
                             childone.add(person2);
                             DefaultMutableTreeNode person4=new DefaultMutableTreeNode("maia");
                             DefaultMutableTreeNode p41=new DefaultMutableTreeNode("medicine");
                             DefaultMutableTreeNode p42=new DefaultMutableTreeNode("pediatri");
                             DefaultMutableTreeNode p43=new DefaultMutableTreeNode("aaaa");
                             p41.add(p42);
                             p41.add(p43);
                             person4.add(p41);
                             childone.add(person4);
                             DefaultMutableTreeNode childtwo=new DefaultMutableTreeNode("fullname");
                             DefaultMutableTreeNode c1=new DefaultMutableTreeNode("merab");
                             DefaultMutableTreeNode c2=new DefaultMutableTreeNode("maia");
                             DefaultMutableTreeNode c3=new DefaultMutableTreeNode("ann");
                             DefaultMutableTreeNode c4=new DefaultMutableTreeNode("david");
                             childtwo.add(c1);
                             childtwo.add(c2);
                             childtwo.add(c3);
                             childtwo.add(c4);
                             DefaultMutableTreeNode childthree=new DefaultMutableTreeNode("email");
                             DefaultMutableTreeNode d1=new DefaultMutableTreeNode("[email protected]");
                             DefaultMutableTreeNode d2=new DefaultMutableTreeNode("[email protected]");
                             DefaultMutableTreeNode d3=new DefaultMutableTreeNode("[email protected]");
                             childthree.add(d1);
                             childthree.add(d2);
                             childthree.add(d3);
                             root.add(childthree);
                             root.add(childone);
                             root.add(childthree);
                             jTree1 = new JTree(root);
                             jScrollPane1.setViewportView(jTree1);
                         //   DefaultTreeModel ddd=(DefaultTreeModel)jTree1.getModel();
                            Comparator<DefaultMutableTreeNode> comp=new Comparator<DefaultMutableTreeNode>() {
                                 public int compare(DefaultMutableTreeNode o1,DefaultMutableTreeNode o2) {
                                      return o1.toString().compareTo(o2.toString());
                            ArrayList<DefaultMutableTreeNode> arr=Collections.list(root.children());
                             Collections.sort(arr,comp);
                             root.removeAllChildren();
                            for(int i=0;i<arr.size();i++)
                                 root.add(arr.get(i));now i want to sort all nodes and leafs that contains this tree , how should i do that? can anyone correct this code so that it sorts all data in this jtree?

    We may extend DefaultMutableTreeNode to implement Comparable
    and override insert so that the relevant nodes are sorted after each insert:
    * TreeSort.java
    import java.util.*;
    import javax.swing.*;
    import javax.swing.tree.*;
    public class TreeSort extends JFrame {
        private JTree jTree1;
        private JScrollPane jScrollPane1;
        public TreeSort() {
            super("TreeSort");
            setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
            setSize(400,300);
            setLocationRelativeTo(null);
            MyNode root = new MyNode("root");
            MyNode childone = new MyNode("id");
            MyNode person1 = new MyNode("david");
            MyNode p11 = new MyNode("java programming");
            MyNode p12 = new MyNode("maths");
            MyNode p13 = new MyNode("c programming");
            person1.add(p11);
            person1.add(p12);
            person1.add(p13);
            childone.add(person1);
            MyNode person3 = new MyNode("ann");
            MyNode p31 = new MyNode("georgian");
            MyNode p32 = new MyNode("english");
            MyNode p33 = new MyNode("russian");
            person3.add(p31);
            person3.add(p32);
            person3.add(p33);
            childone.add(person3);
            MyNode person2 = new MyNode("merab");
            MyNode p21 = new MyNode("veterinar");
            MyNode p22 = new MyNode("building");
            person2.add(p21);
            person2.add(p22);
            childone.add(person2);
            MyNode person4 = new MyNode("maia");
            MyNode p41 = new MyNode("medicine");
            MyNode p42 = new MyNode("pediatri");
            MyNode p43 = new MyNode("aaaa");
            p41.add(p42);
            p41.add(p43);
            person4.add(p41);
            childone.add(person4);
            MyNode childtwo = new MyNode("fullname");
            MyNode c1 = new MyNode("merab");
            MyNode c2 = new MyNode("maia");
            MyNode c3 = new MyNode("ann");
            MyNode c4 = new MyNode("david");
            childtwo.add(c1);
            childtwo.add(c2);
            childtwo.add(c3);
            childtwo.add(c4);
            MyNode childthree = new MyNode("email");
            MyNode d1 = new MyNode("[email protected]");
            MyNode d2 = new MyNode("[email protected]");
            MyNode d3 = new MyNode("[email protected]");
            childthree.add(d1);
            childthree.add(d2);
            childthree.add(d3);
            root.add(childtwo);
            root.add(childone);
            root.add(childthree);
            jTree1 = new JTree(root);
            jScrollPane1 = new JScrollPane();
            jScrollPane1.setViewportView(jTree1);
            add(jScrollPane1);
        public static void main(final String[] args) {
            Runnable gui = new Runnable() {
                public void run() {
                    new TreeSort().setVisible(true);
            //GUI must start on EventDispatchThread:
            SwingUtilities.invokeLater(gui);
    class MyNode extends DefaultMutableTreeNode implements Comparable {
        public MyNode(String name) {
            super(name);
        @Override
        public void insert(final MutableTreeNode newChild, final int childIndex) {
            super.insert(newChild, childIndex);
            Collections.sort(this.children);
        public int compareTo(final Object o) {
            return this.toString().compareToIgnoreCase(o.toString());
    }Edited by: Andre_Uhres on Nov 22, 2008 3:47 PM
    There seems to be a small bug in your original code: childthree is added twice and childtwo isn't added at all.

  • JTree - sorting without collapsing

    i have a JTree which represents data stored in my data layer. if this data changes, i do not only have to update the view part in the tree but also resort it to certain criteria. as far as well, this works fair enough, my problem is, that with every resorting, i remove/add children in the JTree which leads to structure changes. notifying them to the JTree results in the collapsing of all the branches under the event source. how can i prevent that? i'd like to just resort the nodes on one level and leave the collapse/expand of the nodes as it was.
    tnx for the help!

    Thanks for your advice, i tried that but it doesn't seem to work with my code, the effect stays the same. what do i have to change?
    Note:
    snippetComparator cmp = new MyComparator();
    SortedSet sorted = new TreeSet(cmp);
    sorted.addAll(this.children);
    int i = 0;
    for (Iterator it = sorted.iterator(); it.hasNext(); i++) {
    DefaultMutableTreeNode element = (DefaultMutableTreeNode)it.next();
    this.treeModel.removeNodeFromParent(element);
    this.treeModel.insertNodeInto(element, this, i);
    <<snippet
    this is all i do, no manual creation of any TreeEvents.

  • How to sort jTree Nodes

    Hi,
    I am interested in whats the easiest way to sort jTree Nodes alphabetically. Is there any Method to do this directly or may i use something like quicksort?
    Thanks for help
    Mirco

    Thanks Hamed,
    Well, I work in your code, and I post here.
      public static DefaultMutableTreeNode sortTree(DefaultMutableTreeNode root) {
                     for (int i =0; i<root.getChildCount()-1; i++) {
                              DefaultMutableTreeNode node = (DefaultMutableTreeNode) root.getChildAt(i);
                            String nt = node.getUserObject().toString();
                            for (int j=i+1;j<=root.getChildCount()-1;j++)
                                     DefaultMutableTreeNode prevNode = (DefaultMutableTreeNode) root.getChildAt(j);
                                        String np = prevNode.getUserObject().toString();
                                System.out.println(nt+" "+np);
                                if (nt.compareToIgnoreCase(np)>0) {
                                               root.insert(node, j);
                                             root.insert(prevNode, i);
                            if (node.getChildCount() > 0) {
                        node = sortTree(node);
                     return root;
        }and this code I used after include in jtree
      public  void jTreeSortingBegin()
              dn = (DefaultMutableTreeNode)treeModel.getRoot();
              treeModel.reload();
              dn = sortTree(dn);
              treeModel.reload(dn);
        }for exemple
      addObject(jTextField1.getText());
            jTreeSortingBegin();

  • Sorting JTree results in collapse of nodes!

    I've searched and searched and can not find a good example of how to do this. I've found many threads that pose the question with the only answer being something like "I figured it out, thanks anyway".
    My problem:
    I've got a tree, many nodes, many depths. I'd like to sort it. My present algorithm is to traverse the tree and sort the list of children of each node. Once I sort the children, obviously the indexes have changed, so you must correct them.
    I've tried:
    remove all children from the parent
    add the children in sorted order
    If I do that using something like parent.remove and parent.insert, thats fine as long as no children UNDER those children have been expanded. If they have, then I'm not accounting for them and thus the tree continues to show those children under the wrong nodes once the sorted nodes are added.
    If I do that using something like model.removeNodeFromParent and model.insertNodeInto, then the entire tree will collapse up to the node sorted on.
    Either way sucks!
    I've also tried passing Arrays.sort my own comparator that, depending on the compare outcome, will do the removeFromParent and insertNodeInto on the fly. This results in NOT collapsing the entire tree (probably because all nodes are never removed) however it seems like on a descending sort the nodes get messed up. I assumed this was because I was reordering them on the fly in the tree...but now that I'm thinking about it, maybe I was doing something else wrong.
    In any case, is there a standard proceedure for doing this? Why does it seem so difficult? I've spent a good two days on this. Crazy!

    Found this in another thread after all, might be my solution:
         public Vector savePaths() {
              Vector v = new Vector();
              for (int i=0; i<getTree().getRowCount(); i++) {
                   if (getTree().isExpanded(i)) {
                        v.add(getTree().getPathForRow(i));
              return v;
         public void loadPaths(Vector v) {
              for (int i=0; i><v.size(); i++) {          
                   getTree().expandPath((TreePath) v.get(i));
         }

  • Sorting Nodes in JTree

    Can someone please help me. I got this class (MyTreeNode) from this forum but has problem compiling/using it due to my beginners status in Java. My current program is shown below. My problem is that if I change DefaultMutableTreeNode to MyTreeNode the program won't compile as the constructor for MyTreeNode does not take a parameter. Please show my how I can modify this to make use of the new class. I only want to sort the nodes in alphabetical order. If someone has a better way of doing it, please post it here.
    public void xpy()
    DefaultMutableTreeNode node1 = new DefaultMutableTreeNode(field1);
    DefaultMutableTreeNode node2 = new DefaultMutableTreeNode(field2);
    node1.add(node2);
    class MyTreeNode extends DefaultMutableTreeNode
    private static Comparator nodeComparator = new Comparator ()
    public int compare(Object o1, Object o2)
    return o1.toString().compareTo(o2.toString());
    public boolean equals(Object obj)
    return false; //static class -- 1 instance only
    public void add(MutableTreeNode node)
    super.add(node);
    Collections.sort(this.children, nodeComparator); //this.children is a proteced variable reference to the node's vector of children.
    Thanks
    pslloo

    Just add a constructor to your subclass:
    class MyTreeNode extends DefaultMutableTreeNode {
      public MyTreeNode(Object obj) {
        super(obj);
    ...Regards

  • Problem with jtree selection

    Hi all,
    I've got a JTree that represents a sort of data that I'm going to change dynamically in my application. I'd like to reset the selection of the user to the leaf she has selected before the change to the tree structure.
    First of all my tree uses DefaultMutableTreeNode to build each node, and each DefaultMutableTreeNode contains a SingleKeyValue object, that is a key-value object (two strings) and that has an hashcode defined as follows:
    public class SingleKeyValue extends BaseKeyValue{
          * The key of the database object.
         protected String key = null;
          * The description of this object.
         protected String value = null;
         public int hashCode() {
              return this.key.hashCode();
         I hope the hashcode can help me finding the right object even when its value changes (but the key is the same). The construction of the tree is the following:
    DefaultMutableTreeNode root = new DefaultMutableTreeNode("The root");
    // iterate and build each node
    SingleKeyValue object = new SingleKeyValue("key","value");
    DefaultMutableTreeNode node = new DefaultMutableTreeNode(object);
    root.add(node);
    DefaultTreeModel model = new DefaultTreeModel(root);
    JTree  tree = new JTree(model);             
                so the tree is associated to a DefaultTreeModel. Then, when I need to change something in the tree I rebuild the tree and all leafs (with the SingleKeyValues) and rebuild the model:
              TreeModel treeModel = this.myTree.getModel();
              TreeSelectionModel selectionModel = this.myTree.getSelectionModel();
              DefaultTreeModel defaultTreeModel = null;
              DefaultTreeSelectionModel defaultTreeSelectionModel = null;
              TreePath selectedPath = null;
              TreeNode root = null;
              Logger.info("SimpleTreePanel.refreshDatabaseView: treemodel "+treeModel + " selectionModel " + selectionModel);
              if( treeModel != null && treeModel instanceof DefaultTreeModel ) {
                   defaultTreeModel = (DefaultTreeModel) treeModel;
                   root = this.getRoot();
                   // get the selection
                   if( selectionModel != null && selectionModel instanceof DefaultTreeSelectionModel ){
                        defaultTreeSelectionModel = (DefaultTreeSelectionModel) selectionModel;
                        selectedPath = defaultTreeSelectionModel.getSelectionPath();
                        Logger.warn("Selection path of the tree: "+selectedPath);
                   // rebuild the tree
                   defaultTreeModel.setRoot(root);
                   // set the selected element
                   defaultTreeSelectionModel.setSelectionPath(selectedPath);The problem is that after the above code the tree is all closed, no leaf are selected. I guess the problem is with the SingleKeyValueObject because I've tried the default model and it works with, for example, simple strings. Any idea about?
    Thanks,
    Luca

    Here's a complete test program that creates a few SingleKeyValue objetcs (objects with a key and a description) and that then places them into a JTree using the DefaultMutableTreeNode. After that, the selection of user is took and one node is changed, but while the selection is working, the reload/refresh of the tree is not.
    Any idea about how to work on it?
    public class SingleKeyValue extends BaseKeyValue{
          * The key of the database object.
         protected String key = null;
          * The description of this object.
         protected String value = null;
         // constants related to the type of the table this object refers to
         public static final String COMPETENCE = "competenza";
         public static final String FAMILY            = "famiglia_competenza";
         public static final String ROLE              = "ruolo";
         public static final String PROVINCE      = "provincia";
         public static final String KNOWLEDGE  = "titolo_studio";
         public final static String OBJECTIVE      = "obiettivo";
         public final static String PERSON         = "persona";
         public SingleKeyValue( String key, String value, String table){
              super();
              this.key = key;
              this.value = value;
              this.SQLTable = table;
          * @return the key
         public final String getKey() {
              return key;
          * @param key the key to set
         public final void setKey(String key) {
              this.key = key;
          * @return the value
         public final String getValue() {
              return value;
          * @param value the value to set
         public final void setValue(String value) {
              this.value = value;
          * Shows this element.
         public String toString(){
              String ret;
              if( this.showKey ){
                   ret =  this.label + this.key +" { "+ this.value +" }";
              else
                   ret = this.label + this.value;
              // trim the string
              if( ret.length() > this.maxLength )
                   return ret.substring(0 , this.maxLength);
              else
                   return ret;
          * The hash code of this object is the hash code of the key.
          * @return the hash code of the key of the object
         public int hashCode() {
              return this.key.hashCode();
          * Returns true if the object passed is a single key value equals (i.e., with the same key) of this one.
         public boolean equals(Object o){
              if( o== null || (! (o instanceof SingleKeyValue) ) )     return false;
              else
                   return (this.hashCode() == o.hashCode());
         public static void main(String argv[]) throws Exception{
             SingleKeyValue skv1 = new SingleKeyValue("Key1","Description1", SingleKeyValue.COMPETENCE);
             SingleKeyValue skv2 = new SingleKeyValue("Key1a","Description1a", SingleKeyValue.COMPETENCE);
             SingleKeyValue skv3 = new SingleKeyValue("Key1b","Description1b", SingleKeyValue.COMPETENCE);
             SingleKeyValue skv4 = new SingleKeyValue("Key2","Description2", SingleKeyValue.COMPETENCE);
             SingleKeyValue skv5 = new SingleKeyValue("Key2a","Description2a", SingleKeyValue.COMPETENCE);
             SingleKeyValue skv6 = new SingleKeyValue("Key2b","Description2b", SingleKeyValue.COMPETENCE);
             DefaultMutableTreeNode rootNode = new DefaultMutableTreeNode("root");
             DefaultMutableTreeNode node1 = new DefaultMutableTreeNode(skv1);
             DefaultMutableTreeNode node2 = new DefaultMutableTreeNode(skv2);
             DefaultMutableTreeNode node3 = new DefaultMutableTreeNode(skv3);
             DefaultMutableTreeNode node4 = new DefaultMutableTreeNode(skv4);
             DefaultMutableTreeNode node5 = new DefaultMutableTreeNode(skv5);
             DefaultMutableTreeNode node6 = new DefaultMutableTreeNode(skv6);
             rootNode.add(node1);
             node1.add(node2);
             node1.add(node3);
             rootNode.add(node4);
             node4.add(node5);
             node4.add(node6);
             JTree tree = new JTree(rootNode);
             JFrame f = new JFrame("Tree try");
             f.add( new JScrollPane(tree) );
             f.setSize(300,300);
             f.setVisible(true);
             System.out.println("Please select a node within 10 seconds");
             Thread.sleep(10000);
             // now get the user selection
         TreeModel treeModel = tree.getModel();
         TreeSelectionModel selectionModel = tree.getSelectionModel();
         DefaultTreeModel defaultTreeModel = null;
         DefaultTreeSelectionModel defaultTreeSelectionModel = null;
         TreePath selectedPath = null;
         TreeNode root = null;
         if( treeModel != null && treeModel instanceof DefaultTreeModel ) {
              defaultTreeModel = (DefaultTreeModel) treeModel;
              // get the selection
              if( selectionModel != null && selectionModel instanceof DefaultTreeSelectionModel ){
                   defaultTreeSelectionModel = (DefaultTreeSelectionModel) selectionModel;
                   selectedPath = defaultTreeSelectionModel.getSelectionPath();
              // rebuild the tree
              node1.setUserObject(new SingleKeyValue("key20","key changed",SingleKeyValue.FAMILY));
              defaultTreeModel.reload();
              // set the selected element
              defaultTreeSelectionModel.setSelectionPath(selectedPath);
    }

Maybe you are looking for

  • Terminal server does not have any installed licenses

    Good morning! Faced with a some trouble while configuring Terminal Server (Windows Server 2012 based). I selected licensing mode "Per User" and now I see this message: "The Remote Desktop Session Host server is in Per User licensing mode and No Redir

  • Error in Export the ABAP certificate

    Dear gurus: I am executing the template to connect BW with Portal. The proble is this: BI_00_This wizard will execute Postinstall steps of technical configuration of BI-Java > Export the ABAP Certificate Export the ABAP Certificate Description No spe

  • Which is better??? for loop or bulk collect

    declare cursor test is select * from employees; begin open test; loop exit when test%notfound; fetch test into myvar_a(i); CASE A i:=i+1; end loop; close test; open test; fetch test bulk collect into myvar_b; CASE B close test; end; Which case is bet

  • Costs posted through FB01 to General Cost Element in TECO Status

    A general cost element whose system status was changed to TECO on 10/16/2009 has had costs posted from our Travel System in PR1 and also tested posting from FB01 to the same general cost element in a test client sucessfully.  Costs were posted on 12/

  • Cannot import any pictures into iPhoto 5

    A customer has an iMac/G5/1.8Gig running 10.3.9 and iPhoto 5.x. All worked fine until a few weeks ago, when it would not accept any jpg's that she would try to import into her library. I have removed all her pictures from the library, trashed all pre