File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes How to find out reference name from object Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to find out reference name from object" Watch "How to find out reference name from object" New topic
Author

How to find out reference name from object

Manoj Amrutkar
Ranch Hand

Joined: Apr 15, 2006
Posts: 47
Hi,
I have one swing based application in which i have used one JPanel, i have some components on to that JPanel which i got from getComponent method. Now i have the object, what i want is to find out it's reference variable name. Here in i am giving some code to explain it properly



Thanks in advance
Manoj
David McCombs
Ranch Hand

Joined: Oct 17, 2006
Posts: 212
A reference is an implicit pointer to an area of the heap where a certain object exists. How and why would an object know about its reference(s). What would happen if an object had 10 references to it?

The values of cb1 and cb2 is a value that represents an area of memory(ie what it is pointing to). In Java there isn't a practical value for knowing these values since you can not directly manipulate memory.

So when you say values, do you mean the actual value stored in the reference? Or perhaps the title of your post is misleading and you want to know what type of class the actual object is? Or something else?

getComponents() returns an array of components. Did you look at the API listing to see what methods are available? Perhaps putting c[i] in a println statement is what you need.

Component Class
[ November 28, 2006: Message edited by: David McCombs ]

"Should array indices start at 0 or 1? My compromise of 0.5 was rejected without, I thought, proper consideration."- Stan Kelly-Bootle
Kaydell Leavitt
Ranch Hand

Joined: Nov 18, 2006
Posts: 689

If I understand what you're asking, it seems that you want to turn your component that you get back from the components array into a checkbox and then make a call to get the checkbox's label.

String name = c[i].getName();

Is that what you need?

-- Kaydell
[ November 28, 2006: Message edited by: Kaydell Leavitt ]
David Nemeskey
Ranch Hand

Joined: Nov 08, 2006
Posts: 52
Hi,

I do not think you can do that. cb1 and cb2 are fields in your class (IF they are fields, and not method local variables, in which case I think you have no absolutely no means to do it...). However, you add the object that are referenced by the fields to the panel, and get those in the array. These are two different things, on two different levels, and there is no clear relation between them.

So, before I go on, I must ask: why do you need it? I think you have a simpler problem, which may be solved much easier. For example, what is the problem with if (c[i] == cb1) { ... }?

But if you absolutely need to know the name, in your case, you could do something like



BUT you must be aware that this won't work in other cases. For example, what if you have two fields in your class that refer to the same object, say



, and put them to an ArrayList? The code above will not work correctly, as c[i] == st1 and c[i] == st2 will both be true.

This is why I said fields, which are just references, and objects are not the same.
[ November 28, 2006: Message edited by: David Nemeskey ]
Manoj Amrutkar
Ranch Hand

Joined: Apr 15, 2006
Posts: 47
Well thanks a lot guys,
but see i really wanted the values in string format as cb1 and cb2, because i have a pdf corrosponding with each form with certain acrofields mapping to components on to the panel. what i want to do is to take the positions of those acrofields and assign same to corrosponding components on panel.
For that i need to get the components from page with name associated with them compare with acrofields and then assing the position to that component.

hope now the things will be bit clearer.....

i know it is almost close to impossible with java memory restrictions.

but is there any other solution u sees...

Thanks
Manoj
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24184
    
  34

It's not "almost close to impossible with Java memory restrictions." As has already been explained to you, it doesn't even make any sense -- you should reread the above posts if you don't understand why.

In any event, the solution to your problem has already been given above: use Component.getName(). When you create each Component, give it a name that's the same as the variable name. Then you get reference that name later.



Now when you find a Component in an array, you can call getName() and compare that to "cb1".


[Jess in Action][AskingGoodQuestions]
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Is this the question ...

If so, a Map might do the job more efficiently than looping through the components on the panel ...


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: How to find out reference name from object