Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

how to access values from Jtextfields into an array ?

 
divya bhaskaran
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • 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.
 
Nick Meverden
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!