Link to home
Start Free TrialLog in
Avatar of isuhendro
isuhendroFlag for Singapore

asked on

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.
Avatar of isuhendro
isuhendro
Flag of Singapore image

ASKER

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();

myPanel.add(field1);
myPanel.add(field2);

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
Avatar of CEHJ
You could call getComponents on the nearest Container to the JTextField(s) to which you want to get a reference
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.
ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
eg.  field.setName("field1");