finding class parent information

N

nikk_and_jak

Hi,

I am writing some code that looks at a class, and creates some
documents based on class references that it finds using the Reflection
API. I can get all of the compponets in a class, and get references to
each object so that I can query it.

These are swing components mainly I am looking at, and what I would
like to do is to group the components together in a heirachy. For
example, take the following components:

JTabbedPane myTabs
JPanel myPanel ....
JLabel myLabel ....
JButton myButton ....

myPanel.add(myButton);
myPanel.add(myLabel);
myTabs.add(myPanel);

I want to produce a report to say the following type of thing:
JLabel called myLabel belongs to parent JPanel called myPanel
JButton called myButton belongs to parent JPanel called myPanel
JPanel called myPanel belongs to the parent JTabbedPane myTabs

I can seem to get the class type (e.g. JPanel) for a parent easilly,
and some other info, but I cant get the name for it (e.g. myPanel)

The only way I could think to get the info was using the Accessibility
methods.
In my code, I do the following to get the references to the
AccessableParent:

AccessibleContext context = myLabel.getAccessibleContext();
Accessible parent =
myLabel.getAccessibleContext().getAccessibleParent();

Then I try the following:

1) parent.getAccessibleContext().getAccessibleName()
2) parent.getClass().toString()
3) parent.toString()

And get the following output
1) = null
2) = class javax.swing.JPanel
3)
[,0,0,155x36,layout=java.awt.FlowLayout,alignmentX=null,alignmentY=null,border=,flags=9,maximumSize=,minimumSize=,preferredSize=]

The only time I get any useful info back is if a JPanel has a titled
border - I can access the title text by calling 1) above.

I thought that something simpler may work, and tried:

Container parentCont = myLabel.getParent();
System.out.println("Parent name = "+parentCont.getName());

However, this gets null!

Any help would be appreciated.
 
A

Alan Krueger

I want to produce a report to say the following type of thing:
JLabel called myLabel belongs to parent JPanel called myPanel
JButton called myButton belongs to parent JPanel called myPanel
JPanel called myPanel belongs to the parent JTabbedPane myTabs

I can seem to get the class type (e.g. JPanel) for a parent easilly,
and some other info, but I cant get the name for it (e.g. myPanel)

You can't. myPanel is the name of a reference to the object, not the
name of the object. Even if you could get the name of a reference, you
can have multiple references to the same object: which name would you use?
 
N

nikk_and_jak

I have solved this - in a strange way but none the less you can get the
reference to the object!
What I had to do first is cycle through all of the Field objects
returned by the reflection call, as from here you can get the reference
to the object, and its name. I store these in a hashtable with the
object as the key.

Then when I get the object instance from getParent() call, I can use
the object to look up the name (e.g. myPanel) in the Hashtable.
 
J

Joan

I have solved this - in a strange way but none the less you can get the
reference to the object!
What I had to do first is cycle through all of the Field objects
returned by the reflection call, as from here you can get the reference
to the object, and its name. I store these in a hashtable with the
object as the key.

Then when I get the object instance from getParent() call, I can use
the object to look up the name (e.g. myPanel) in the Hashtable.

How nice for you. Do you have a question?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top