简体   繁体   中英

How I Show Windows Registry in jTree?

I am using java swing.I want to show windows registry in jTree how it is possible.I am new in java please give me a solution.Actually i want to delete a particular windows registry key i done it but i can not show it in jtree means i show the structure before regkey deletion and after deletion.

I had similar problem, and wasn't able to find something ready-to-use. After some reading I wrote a TreeModel implementing class to do this.
Class can be used to convert any TreePath list into tree, eg:

A->B->C->D
A->B->E->F
A->G->H->J

will become:

A+
 |B+
 | |C+
 | | |D
 | |E+
 |   |F
 |G+
   |H+
     |J

Here it is:

    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.Iterator;
    import java.util.NoSuchElementException;
    import javax.swing.event.TreeModelListener;
    import javax.swing.tree.DefaultMutableTreeNode;
    import javax.swing.tree.TreeModel;
    import javax.swing.tree.TreePath;

    /**
     *
     * @author Majkel
     */
    public class TreePathsTreeModel implements TreeModel {
      private final ArrayList<TreePath> paths;
      private final String root;

      public TreePathsTreeModel(String root, TreePath[] paths) {
        this.root = root;
        this.paths = new ArrayList<TreePath> (Arrays.asList(paths));
      }


      @Override
      public Object getRoot() {
        return this.root;
      }

      @Override
      public Object getChild(Object parent, int index) {
        try {
          return getChildren(parent).get(index);
        } catch (IndexOutOfBoundsException ex) {
          return null;
        }
      }

      @Override
      public int getChildCount(Object parent) {
        return getChildren(parent).size();
      }

      @Override
      public boolean isLeaf(Object node) {
        for (int i = 0; i < paths.size(); i++) {
          TreePath treePath = paths.get(i);
          if (treePath.getLastPathComponent().equals(node))
            return true;
        }
        return false;
      }

      // This method is only required for editable trees, so it is not
      // implemented here.
      @Override
      public void valueForPathChanged(TreePath path, Object newValue) {
        //throw new UnsupportedOperationException("Not supported yet.");
      }

      @Override
      public int getIndexOfChild(Object parent, Object child) {
        return getChildren(parent).indexOf(child);
      }

      // This TreeModel never fires any events (since it is not editable)
      // so event listener registration methods are left unimplemented
      @Override
      public void addTreeModelListener(TreeModelListener l) {
        //throw new UnsupportedOperationException("Not supported yet.");
      }

      @Override
      public void removeTreeModelListener(TreeModelListener l) {
        //throw new UnsupportedOperationException("Not supported yet.");
      }
              //search all paths in list for given object 
              //return every item one level further than it
      private ArrayList<String> getChildren(Object parent) {
        ArrayList<String> children = new ArrayList<String>();
        for (int i = 0; i < this.paths.size(); i++) {
          ArrayList<Object> pathObjects = new ArrayList<Object>( Arrays.asList(this.paths.get(i).getPath()) );
          for (Iterator<Object> it = pathObjects.iterator(); it.hasNext();) {
            Object parentCandidate = it.next();
            if (parentCandidate.equals(parent)) {
              Iterator<Object> checker = it;
              try {
                String child = new DefaultMutableTreeNode( checker.next() ).toString();
                if ( !children.contains(child) )
                  children.add (child);
              } catch (NoSuchElementException ex) {

              } 
            }
          }
        }
        return children;
      }

    }

It is vulnerable to duplicate entries, eg:

A->B->C->D
A->C->E->F
A->G->H->J

will become:

 A+
  |B+
  | |C+
  |   |D
  |   |E+
  |     |F
  |C+
  | |D
  | |E+
  |   |F
  |G+
    |H+
      |J

Maybe I'll fix it later, or maybe someone has an idea how to do it? An example use:

TreePath tpOne = new TreePath(new String [] {"A", "B", "C", "D"});
TreePath tpTwo = new TreePath(new String [] {"A", "B", "E", "F"});
TreePath tpThree = new TreePath(new String [] {"A", "G", "H", "J"});

TreeModel model = new TreePathsTreeModel("A", TreePath[]{tpOne, tpTwo, tpThree}  );
JTree tree = new JTree(model);

Take a look at " read/write to Windows Registry using Java "

Update: To display the registry in a JTree you will need to provide a TreeModel .

I would start with the FileSystemModel from Oracle's Understanding the TreeModel and replace the File calls with calls into the registry library you are using.

The win32 registry can be quite large, so you'll probably need to add a TreeExpansionListener to release branches that have been closed. Here's an Oracle article on tree expansion listeners .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM