1

I want to display all the fields in an object and all the fields of the field etc, etc. So basically the "graph" or hierarchy of an object.

JTree would suffice, but is there anything else that someone might recommend in order to display an object's graph in Java Swing?

To clarify, I want to do this programmatically, and I want to do this with ANY java object.

This question is pretty much the same as:

How to display tree hierarchy in Java?

Community
  • 1
  • 1
Alexander Mills
  • 90,741
  • 139
  • 482
  • 817
  • 1
    You could serialize the object to XML and construct a tree by translating descendants to tree elements. – Jeroen Vannevel Sep 25 '13 at 01:24
  • 1
    Like [this](http://stackoverflow.com/a/6674624/230513)? – trashgod Sep 25 '13 at 01:28
  • @trashgod When was that added! Nice find – MadProgrammer Sep 25 '13 at 01:31
  • 1
    @trashgod I believe he means the fields of any object (and in turn the fields of each Object field, and so forth), not the Swing hierarchy. – FThompson Sep 25 '13 at 01:32
  • yes, any object, not a preexisting hierarchy. – Alexander Mills Sep 25 '13 at 01:40
  • @Jeroen, sounds interesting but I need a visualization library of some variety to display it. JTree is an option, but I am wondering if there are any good alternatives. – Alexander Mills Sep 25 '13 at 01:43
  • for instance, JGraph, http://www.jgraph.com/ – Alexander Mills Sep 25 '13 at 01:44
  • @JeroenVannevel How would that approach support looping references (i.e. two objects that hold references to each other)? – FThompson Sep 25 '13 at 02:24
  • For my purposes, if two objects both referenced each other, it wouldn't matter. Each object's graph would be displayed separately. So basically only one way relationships, not two way relationships. – Alexander Mills Sep 25 '13 at 02:33
  • @Vulcan: when a property of the main class holds a reference to a type of the main class, you shouldn't expand the latter one since that adds no additional information. Unless you also want to take the values into account, but you'll always hit a limitation with circular references. It's a matter of cutting off at one point; my suggestion would be to avoid re-displaying the main object. – Jeroen Vannevel Sep 25 '13 at 02:36
  • @MadProgrammer: Not sure; also seen [here](http://www.oracle.com/technetwork/java/javase/awt-138016.html#gchlz). – trashgod Sep 25 '13 at 10:05

1 Answers1

1

What you are looking for is to serialize an object graph into a data structure that can be displayed in a JTree. This is a very complex task with a lot of work involved. To do this properly you will need to perform the following (this is just a summary):

  • determine the data structure you will use (maybe an implementation of TreeNode or something equivalent) to represent objects and fields

  • explore the object graph recursively using reflection. For each class explore the fields including those in the full chain of inheritance; then for each field do the same with its class if it is not a primitive type; also make sure you call setAccessible(true) on each non-public field to actually access it

  • handle object reference cycles, you don't want to loop forever and end up with a StackOverflowError nor with an inifnite tree. You will need to figure a way to link references to an object to its actual content in your JTree. This is one of the cases where I found IdentityHashMap to be useful and desirable.

My recommendation is not to do it from scratch. You could find inspiration from various sources, for example: the source of java.io.ObjectOutputStream and its accompanying classes, or other open source serialzation frameworks such as XStream or Kryo or a bunch of others.

I personally have implemented such a framework for specific purposes some time ago, which is why I have an idea of the excrutiating pain it can be. You can find the source here (start with the JPPFObjectOutputStream class).

Lolo
  • 4,277
  • 2
  • 25
  • 24