[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Declaring JtextField dynamically and setting size and name inside of loop

Posted on 2004-11-24
14
Medium Priority
?
571 Views
Last Modified: 2008-03-06
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,

0
Comment
Question by:dkim18
  • 7
  • 6
14 Comments
 
LVL 9

Assisted Solution

by:OBCT
OBCT earned 240 total points
ID: 12670599
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
 
LVL 37

Accepted Solution

by:
zzynx earned 480 total points
ID: 12672737
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
 

Author Comment

by:dkim18
ID: 12721587
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 37

Expert Comment

by:zzynx
ID: 12723709
>> but I had null pointer exception
Please show us the code you have so far.
So, you're using the array way?
0
 

Author Comment

by:dkim18
ID: 12723744
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
 
LVL 37

Expert Comment

by:zzynx
ID: 12724080
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
 

Author Comment

by:dkim18
ID: 12726589
           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
 
LVL 37

Expert Comment

by:zzynx
ID: 12726650
>> fieldPanel.add(textFields[i]);   <-------------NullPointerException

Sure fieldPanel is already initialized?

fieldPanel = new JPanel();
0
 

Author Comment

by:dkim18
ID: 12726904
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
 
LVL 37

Expert Comment

by:zzynx
ID: 12726952
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
 

Author Comment

by:dkim18
ID: 12727021
labelPanel is OK. It indicates fieldPanel.

fieldPanel.add(textFields[i]); <----
0
 
LVL 37

Assisted Solution

by:zzynx
zzynx earned 480 total points
ID: 12727178
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
 

Author Comment

by:dkim18
ID: 12727227
Stupid me too!
Thanks!!
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12727266
Thanks for accepting
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses
Course of the Month18 days, 3 hours left to enroll

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question