how to find the class of an object?

S

Sean Anderson

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
 
B

Betty

Sean Anderson said:
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) {...}
 
R

Ross Bamford

Sean Anderson said:
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
 
G

Guest

Sean Anderson said:
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();
}
 

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,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top