wood burning stoves 2.0*
The moose likes Swing / AWT / SWT and the fly likes getName() for a JCompnent returns null Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "getName() for a JCompnent returns null" Watch "getName() for a JCompnent returns null" New topic
Author

getName() for a JCompnent returns null

Nate Lockwood
Ranch Hand

Joined: Feb 22, 2010
Posts: 83
I need to get the name of a JComponent but it returns null UNLESS I assign the name in code. Why?



Why should I need to add the component's name in code? It appears a bit "unclean".

In this case the JComponent is a JTextField but I get the same result if I cast to JTextField.

Kemal Sokolovic
Bartender

Joined: Jun 19, 2010
Posts: 825
    
    5

getName() returns a proper value only if you invoked setName() in the first place.


The quieter you are, the more you are able to hear.
Nate Lockwood
Ranch Hand

Joined: Feb 22, 2010
Posts: 83
Kemal Sokolovic wrote:getName() returns a proper value only if you invoked setName() in the first place.


There is no other way to get the name? I already gave it a name when I created an instance and I also need to "tell" it what its name is and without a typo, too? And if I want to change the name I need to be quite careful as some will be in quotes and some not. I'm using Eclipse and when I hover over getName() there is no mention of setName() in the popup, alas.

Thanks,
Nate
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Were you under the impression that the name of a variable which contains a reference to an object is automatically the name of that object?

No. Objects don't have names. Variables do have names, but you can't get those names by looking at objects to which the variables happen to refer at the current time.
Nate Lockwood
Ranch Hand

Joined: Feb 22, 2010
Posts: 83
Paul Clapham wrote:Were you under the impression that the name of a variable which contains a reference to an object is automatically the name of that object?

No. Objects don't have names. Variables do have names, but you can't get those names by looking at objects to which the variables happen to refer at the current time.


I guess I did. I can retrive other characteristics of the object pointed to by jtf so I thought "why not the name", especially as there is a getName() method (and so many others). Now I see that it is not possible but for my immediate purpose it's just fine. I would have had to parse the name since I have several JTextFields with similar names but all I want is the sub strings "SensorA", "SensorB", etc.. I'll just name them.



Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4523
    
    5

Nate Lockwood wrote:I have several JTextFields with similar names but all I want is the sub strings "SensorA", "SensorB", etc..


Rather looks like a use case for an array or Collection, that.


luck, db
There are no new questions, but there may be new answers.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: getName() for a JCompnent returns null
 
Similar Threads
problem using XMLBeans from a stateless session bean (WSAD 5.1)
BoxLayout not working as advertised?
Please help me check this regex
data type in java
form design