Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

Dynamic JTextFields Help

Hey all :WAVE: ,

Having a slight problem displaying dynamic JTextFields in my java app. Problem as follows:

A user selects an item from a combo box and presses 'ok'. All the details of that item (stored in a dbase) need to then be displayed in JTextFields on the interface. The number of JTextField required is dependent on the number of columns in the table which holds details on the item which has been choosen by the user.

My problem is that after i have retrieved all the details and set the JTextFields with the data - it doesnt seem to display the fields on the screen!

I have tried a number of ways - the latest way is shown below in the code snippet:

private void selectRiskReview(){

// gets all details of the item selected from dbase
ResultSet rs = d.getRiskDetails (c.colRiskID + " = " + r.getRiskID ());
// retrieves the number of columns = number of jtext field required
Integer noCols=d.getNoOfCols (rs);

try{
while(rs.next()){
for (int i=1; i<noCols+1; i++){
String fieldName = "jDynamicText" + i;
JTextField tName = new JTextField();
tName.setName (fieldName);
tName.setText (rs.getString(i));
tName.setVisible(true);
jReviewScreen.add(tName);
}
}

} catch (SQLException e){
System.out.println (e);
}
jReviewScreen.repaint();

jReviewScreen.setVisible (true);
}

Any ideas why the newly added text fields are not showing?

Any help will be v much appreciated!

Thanks in advance!

ev
[1871 byte] By [evian] at [2007-11-11 8:50:27]
# 1 Re: Dynamic JTextFields Help
You only ever really create 1 text field. You need to make an array.

JTextField[] fields = new JTextField[noCols];

for (int i=0;i<fields.length;i++)
{
fields[i] = new JTextField(rs.getString(i));
jReviewScreen.add(fields[i]);
}
Phaelax at 2007-11-11 22:34:16 >
# 2 Re: Dynamic JTextFields Help
Thanks Phaelax,

Have tried using the array way before (and just in case i had made any errors used your code as well) and it still does not show the text boxes on the screen...

I'm completely lost as to why this may happen!!!...Ive repainted the screen,validated and set visible true-and nothing seems to work!!!
evian at 2007-11-11 22:35:16 >
# 3 Re: Dynamic JTextFields Help
Why not reconsider your design, and go for a one column scrollable JTable. You could have it in a popup dialog (always-on-top) or in a dynamic sidebar of your application mainframe.
sjalle at 2007-11-11 22:36:19 >
# 4 Re: Dynamic JTextFields Help
hi sjalle,

thanks for the reply! :)...

i did redesign my code...and the dynamic text fields are now working :)...

I'm still working on the code - in terms of getting the layout right (dynamic data entry forms), but once i get it all right i will post the code so others having the same problem can take a look and see what they are doing incorrectly!

Thanks again :)

ev.
evian at 2007-11-11 22:37:14 >