Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Dynamic creation of JTextField

 
Nigel Browne
Ranch Hand
Posts: 703
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I trying to write a program that takes a commandline argument and then dynamically creates a GUI. I want the user to enter a number. This number is then to be used to create the same number of JTextFields twice in arrays. These two arrays are then to be added to a GridLayout. I keep getting ArrayIndexOutOfBounds exception when ying to add the first array.
How do you add an array of JTextFields to GridLayout?
 
Meghna ks
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try adding the textfields to a panel by setting the panel's layout to GridLayout. This should take care of arranging the textfields in the gridlayout.
Panel.setLayout(new GridLayout(0, 0, 0, 2);
Meghna
 
Nigel Browne
Ranch Hand
Posts: 703
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Meghna ks:
Try adding the textfields to a panel by setting the panel's layout to GridLayout. This should take care of arranging the textfields in the gridlayout.
Panel.setLayout(new GridLayout(0, 0, 0, 2);
Meghna

I have been doing this:
This I am hoping creates a two column grid with exactly the correct number of rows for the JTextFields.
My problem lies within this code loop:

 
Swamy Vatti
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This should be absolutely a program logic problem.
If u handle to create sufficient number of grids and add it, I think there should not be any problem.
 
Paul Stevens
Ranch Hand
Posts: 2823
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JTextField test[] = new JTextField();
mainpanel.add(test[i]);
Try creating your array of JTextFields outside your loop. Also create your components within the loop and add.
Outside
JTextField test[] = new JTextField[noOfFields];
Within loop:
test[i] = new JTextField();
mainPanel.add(test[i]);
Make sure the array is not a local array.
 
Javaoops
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try out


 
Nigel Browne
Ranch Hand
Posts: 703
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your help everyone
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic