• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 294
  • Last Modified:

how to identifiy JTextField object at runtime, since the reference name is not stored at class file?

hello experts,

understand that for below code:
  JTextField field1 = new JTextField();

field1 will not be stored inside the class file, but only at source code file.

Then how could I identify this object at runtime? In other words, is there any way to give id to "field1" object that can be read at runtime by other codes  (since the reference name "field1", will not be stored at runtime)

Or should I override JTextField into a new class and create a new attributes for id? Is there any for the existing JTextField?

Thanks & regards.
1 Solution
isuhendroAuthor Commented:
btw to clarify..
for example

at class A i have code like below:

JPanel myPanel = new JPanel();
JTextField field1  = new JTextField();
JTextField field2  = new JTextField();


my question is related to an inner class, named class B, inside class A.
Inside class B, I am trying to write code which will return a collection whose members are a pair of fieldname and its value, e.g ("field1", "valueoffield1"), ("field2", "valueoffield2").
The problem is as i told you, i could not get the id "field1", "field2", etc
You could call getComponents on the nearest Container to the JTextField(s) to which you want to get a reference
Mayank SAssociate Director - Product EngineeringCommented:
You can expose it as a member of the class A:

public JTextField getFirstTextField ()
  return field1 ;


You can either expose all of them this way (using multiple get methods) to be accessible from outside or create them in an array and expose the array in one method.
eg.  field.setName("field1");
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Tackle projects and never again get stuck behind a technical roadblock.
Join Now