my dog learned polymorphism*
The moose likes Swing / AWT / SWT and the fly likes Dynamic creation of JTextField Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Dynamic creation of JTextField" Watch "Dynamic creation of JTextField" New topic
Author

Dynamic creation of JTextField

Nigel Browne
Ranch Hand

Joined: May 15, 2001
Posts: 673
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

Joined: Mar 15, 2001
Posts: 122
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

Joined: May 15, 2001
Posts: 673
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

Joined: May 02, 2001
Posts: 57
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.


Swamy
Paul Stevens
Ranch Hand

Joined: May 17, 2001
Posts: 2823
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

Joined: Jun 21, 2001
Posts: 57
try out


Nigel Browne
Ranch Hand

Joined: May 15, 2001
Posts: 673
Thanks for your help everyone
 
jQuery in Action, 2nd edition
 
subject: Dynamic creation of JTextField