Declaring JtextField dynamically and setting size and name inside of loop

Hi!

I am trying to declare array of JtextField and give different name for each object inside of loop. Something like:
---------------------
textFields = new JTextField[array.length];

for (int i = 0; i < array.length; i++) {      
      somePanel.add(textFields[i] ----->????


---------------------
thanks,

dkim18Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

OBCTCommented:
The only way you can make (kind of) dynamic instance names is to put them in a Map.

E.g.

Map textFields = new HashMap();
int amountOfFields = 10; // Change this to suite your needs

for (int i = 0; i < amountOfFields; i++)
{
    textFields.put("TextField" + i, new JTextField());
}
0
zzynxSoftware engineerCommented:
So to extend OBCT's example:

Map textFields = new HashMap();
int amountOfFields = 10; // Change this to suite your needs

for (int i = 1; i <= amountOfFields; i++)
{
    JTextField field = new JTextField();
    textFields.put("textField" + i, field);
    somePanel.add(field);
}

Then if you want
1) to set the text in field 5:

    ((JTextField)textFields.get("textField5")).setText("Whatever");

2) to get the text in field 5:

    String theText = ((JTextField)textFields.get("textField5")).getText();


Of course your array could also work (but in that case you can't get the JTextField by name, just by index):

int maxFields = 10; // Change this to suite your needs
JTextField textFields[] = new JTextField[maxFields];

for (int i = 0; i < textFields.length; i++)
     somePanel.add(textFields[i]);

1) to set the text in field 5:
     
     textFields[4].setText("....");

2) to get the text in field 5:

     String theText = textFields[4].getText();
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
dkim18Author Commented:
Thanks for all your help. This works, but now I have another problem. I need to create reset button  clear up all text fields.
I tried  textFields[i].setText("...") but I had null pointer exception.  I also have an error when I tried to add  textFields[i] to panel and I do not know why. is there any way to do it? Since I am asking help for another problem, I will increase points.

thanks in advance..
0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

zzynxSoftware engineerCommented:
>> but I had null pointer exception
Please show us the code you have so far.
So, you're using the array way?
0
dkim18Author Commented:
I used Map and then I was trying to implement reset button. I couldn't figure out to clear text fields, so I changed it to array and I am having this error. I do not have the code now, but I can show you tomorrow morning.

From your code, I just tried to put text field on panel inside of loop:

somePanel.add(textFields[i])
0
zzynxSoftware engineerCommented:
This should simply work:

int maxFields = 10; // Change this to suite your needs
JTextField textFields[] = new JTextField[maxFields];     // <<< I guess you forgot the right side of this one

for (int i = 0; i < textFields.length; i++)
     somePanel.add(textFields[i]);

If you don't have that right side, you'll get null pointer errors.
0
dkim18Author Commented:
           JTextField textFields[] = new JTextField[files.length];
            for (int i = 0; i < files.length; i++) {
            
                  topDirArr[i] = files[i].toString().substring(files[i].toString().lastIndexOf("\\")+1,files[i].toString().length());
                  label = new JLabel(topDirArr[i], JLabel.RIGHT);
                  labelPanel.add(label);            
                  
                  fieldPanel.add(textFields[i]);   <-------------NullPointerException
+++++++++++
that is exactly what I am doing, but still NullPointerException there..
0
zzynxSoftware engineerCommented:
>> fieldPanel.add(textFields[i]);   <-------------NullPointerException

Sure fieldPanel is already initialized?

fieldPanel = new JPanel();
0
dkim18Author Commented:
yes.

JPanel fieldPanel = new JPanel(new GridLayout(files.length,1));
...
...
          JTextField textFields[] = new JTextField[files.length];
          for (int i = 0; i < files.length; i++) {
         
               topDirArr[i] = files[i].toString().substring(files[i].toString().lastIndexOf("\\")+1,files[i].toString().length());
               label = new JLabel(topDirArr[i], JLabel.RIGHT);
               labelPanel.add(label);          
+++++++++++
0
zzynxSoftware engineerCommented:
Do you have the null pointer here
         >>               fieldPanel.add(textFields[i]);   <-------------NullPointerException
         >> +++++++++++
or here
         >>               labelPanel.add(label);          
         >> +++++++++++
?

Is there a line indication in your stack trace. To which line does it point?

0
dkim18Author Commented:
labelPanel is OK. It indicates fieldPanel.

fieldPanel.add(textFields[i]); <----
0
zzynxSoftware engineerCommented:
Aahhh. Of course. Stupid me.

This
        >> JTextField textFields[] = new JTextField[files.length];

is not enough. It just reserves x spaces to store a reference to a JTextField in.

Replace the above by

         JTextField textFields[] = new JTextField[files.length];  // unchanged
         for (int i=0; i<files.length; i++)                                  
            textFields[i] = new JTextField();               // initialize them

and your NPE will be gone.
0
dkim18Author Commented:
Stupid me too!
Thanks!!
0
zzynxSoftware engineerCommented:
Thanks for accepting
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.