Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

getName() for a JCompnent returns null

 
Nate Lockwood
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 825
5
Java Python Ruby
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
getName() returns a proper value only if you invoked setName() in the first place.
 
Nate Lockwood
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21107
32
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5126
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic