• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

how to access values from Jtextfields into an array ?

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hello forum,

I am a novice to java and is finding it very difficult to understand the intricacies of java.Your help will be valuable to me.

I have a series of randomly generated textfields( generated inside an event handler) and the user input has to be stored inside an array like ,parameters[],at the click of a button.I will send my code.Plz help me out with your valuable suggestions.
 
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you said it's a list of params, maybe a Map with KEY/VALUE would be better design?

anyways, you can just grab all the JTextFields from the JPanel...

javax.swing.JPanel panel = new javax.swing.JPanel();
Component[] cList = panel.getComponents();
for (int x = 0; x < cList.length; x++) {
if (cList[x] instanceof javax.swing.JTextField) {
String value = ((javax.swing.JTextField) cList[x]).getText();
//PUT VALUE INTO CONTAINER
}
}
 
divya bhaskaran
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hello friend,thank you for your help,

Your idea is quite new to me.I tried printing the string data "value" using system.out.println inside the same loop,but it wont get displayed.I need those values in an array like this.
Sting[] data;
What could be the problem?Plz help.
 
Nick Meverden
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
java.awt.Component[] cList = panel.getComponents();

int size = cList.length;
String[] data = new String[size];

for (int x = 0; x < size; x++) {
if (cList[x] instanceof javax.swing.JTextField) {
String value = ((javax.swing.JTextField) cList[x]).getText();
data[x] = value;
}
}

//at this point data[] is populated with the information you wan't.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic