how to find the class of an object?

Discussion in 'Java' started by Sean Anderson, May 15, 2005.

  1. If I pass an object reference(JComponent) to a method I want to be able to
    find out what class of omponent it is; JTextField, JComboBox etc...

    Is there a way to do this? No "comparable" bull please.

    Sean
     
    Sean Anderson, May 15, 2005
    #1
    1. Advertising

  2. Sean Anderson

    Betty Guest

    "Sean Anderson" <> wrote in message
    news:9Owhe.855$...
    > If I pass an object reference(JComponent) to a method I want to be able to
    > find out what class of omponent it is; JTextField, JComboBox etc...
    >
    > Is there a way to do this? No "comparable" bull please.
    >
    > Sean
    >

    Component bb = MORE_BTNPANEL_REFERENCES[mwindow].getComponent(index);

    if(bb instanceof JLabel) {...}
     
    Betty, May 15, 2005
    #2
    1. Advertising

  3. Sean Anderson

    Ross Bamford Guest

    On Sat, 2005-05-14 at 19:53 -0500, Betty wrote:
    > "Sean Anderson" <> wrote in message
    > news:9Owhe.855$...
    > > If I pass an object reference(JComponent) to a method I want to be able to
    > > find out what class of omponent it is; JTextField, JComboBox etc...
    > >
    > > Is there a way to do this? No "comparable" bull please.
    > >
    > > Sean
    > >

    > Component bb = MORE_BTNPANEL_REFERENCES[mwindow].getComponent(index);
    >
    > if(bb instanceof JLabel) {...}


    and more generally,

    Class bbClass = bb.getClass();

    See http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Class.html

    Also, How on earth *would* you do this with comparable?

    Ross

    --
    [Ross A. Bamford] [ross AT the.website.domain]
    Roscopeco Open Tech ++ Open Source + Java + Apache + CMF
    http://www.roscopec0.f9.co.uk/ + in
     
    Ross Bamford, May 15, 2005
    #3
  4. Sean Anderson

    Guest

    "Sean Anderson" <> wrote in message
    news:9Owhe.855$...
    > If I pass an object reference(JComponent) to a method I want to be able to
    > find out what class of omponent it is; JTextField, JComboBox etc...
    >
    > Is there a way to do this? No "comparable" bull please.


    How about

    public static void someMethod(Object obj) {
    Class theClass = obj.getClass();
    String nameOfClass = theClass.getName();
    }
     
    , May 15, 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. E11
    Replies:
    1
    Views:
    4,952
    Thomas Weidenfeller
    Oct 12, 2005
  2. Ami
    Replies:
    3
    Views:
    442
    Tomás
    Feb 27, 2006
  3. Wybo Dekker
    Replies:
    1
    Views:
    399
    Yukihiro Matsumoto
    Nov 15, 2005
  4. vdvorkin
    Replies:
    0
    Views:
    440
    vdvorkin
    Feb 10, 2011
  5. vdvorkin
    Replies:
    3
    Views:
    870
    vdvorkin
    Feb 14, 2011
Loading...

Share This Page