• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

getName() for a JCompnent returns null

 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Bartender
Posts: 825
5
Python Ruby Java
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.



 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
reply
    Bookmark Topic Watch Topic
  • New Topic