Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #36806

Custom JTree does this make sense?

Newsgroups comp.lang.java.programmer
Date 2017-07-06 10:52 -0700
Message-ID <e7950596-536f-46cd-8af7-34bf7b12fcdc@googlegroups.com> (permalink)
Subject Custom JTree does this make sense?
From Eric Douglas <e.d.programmer@gmail.com>

Show all headers | View raw


I have a custom class which extends JTree.
Every node on my tree is required to be of my custom node class which extends DefaultMutableTreeNode.

I was trying to think of how to fix a problem I had linking nodes when I noticed the implementation.  DefaultMutableTreeNode stores it's children in a Vector, so you can have one child in more than one parent.  Each node just needs to implement the TreeNode interface which requires each node have at least one parent with a getParent() method returning a single TreeNode object.

So I can link to multiple parents by simply adding methods to my custom node in addition to the interface.  Like a JTable has getSelectedRow() returning a single row even if you can have multiple rows selected, I just need a getParent() method (returns first parent, or null if no parents) and a getParents() method, a setParent() to set one, or clearParents() and addParent() to support multiples.

addParent() just needs to check the node you're adding is not already in the heirarchy of the current node or any of it's other parents.

I'm trying to use a tree to describe a bill of material.  For instance each node represents a part.  The root node may be a chair.  The chair may require a different part for the left arm and right arm, but within each arm there's another constructed part which is the same in both.  Any change to one of those should automatically update the other(s).

Previously I've just been creating new nodes for each part including the ones that match, and maintaining lists of nodes with ArrayLists and HashMaps to manually figure out if one changes, if there's any other nodes for the same part that should get the same change.  That just sounds ridiculous now.

A -> B -> A = invalid tree

A -> B -> D
       -> E
  -> C -> D
       -> F = valid tree
Any changes to part D should update all references.

Back to comp.lang.java.programmer | Previous | Next — Next in thread | Find similar | Unroll thread


Thread

Custom JTree does this make sense? Eric Douglas <e.d.programmer@gmail.com> - 2017-07-06 10:52 -0700
  Re: Custom JTree does this make sense? Eric Douglas <e.d.programmer@gmail.com> - 2017-07-06 11:13 -0700
    Re: Custom JTree does this make sense? Martin Gregorie <martin@address-in-sig.invalid> - 2017-07-06 21:33 +0000
      Re: Custom JTree does this make sense? Eric Douglas <e.d.programmer@gmail.com> - 2017-07-12 08:45 -0700
        Re: Custom JTree does this make sense? Martin Gregorie <martin@address-in-sig.invalid> - 2017-07-12 16:56 +0000
          Re: Custom JTree does this make sense? Eric Douglas <e.d.programmer@gmail.com> - 2017-07-12 10:27 -0700
            Re: Custom JTree does this make sense? Martin Gregorie <martin@address-in-sig.invalid> - 2017-07-12 18:31 +0000

csiph-web