finding class parent information

Discussion in 'Java' started by nikk_and_jak@btinternet.com, Jul 12, 2005.

  1. Guest

    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.
     
    , Jul 12, 2005
    #1
    1. Advertising

  2. Alan Krueger Guest

    wrote:
    > 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?
     
    Alan Krueger, Jul 13, 2005
    #2
    1. Advertising

  3. Guest

    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.
     
    , Jul 21, 2005
    #3
  4. Joan Guest

    <> wrote in message
    news:...
    >
    > 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?
     
    Joan, Jul 21, 2005
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Roy
    Replies:
    2
    Views:
    3,124
  2. E11
    Replies:
    1
    Views:
    4,801
    Thomas Weidenfeller
    Oct 12, 2005
  3. seanacais
    Replies:
    1
    Views:
    318
    Bruno Desthuilliers
    Sep 9, 2008
  4. metal
    Replies:
    8
    Views:
    512
    Bruno Desthuilliers
    Oct 30, 2009
  5. RichardOnRails
    Replies:
    2
    Views:
    97
    RichardOnRails
    Sep 14, 2008
Loading...

Share This Page