Declare a List
before the for
loop and change the else statement to
else {
JTextField tf = new JTextField(1);
list.add(tf);
p3.add(tf);
}
After that iterate over the list
and get the text from each element.
Question
I'm have a grid of textfields and labels and i want to know what would i have to do to get the input from the textfields. I know how to do it if it was declared? i think is the word for it, but this is a bit different. i dont know how to recieve the input from something that hasnt been given a name. this was something i realized after the matter.
A sample from a class of what i'm talking about
for (int c = 0; c < 9; c++) {
p3 = new JPanel(new GridLayout(3, 3));
p3.setBorder(lineBorder);
for (int d = 0; d <= 8; d++) {
if (d == 0) {
p3.add(new JLabel("5", JLabel.CENTER));
} else if (d == 5) {
p3.add(new JLabel("8", JLabel.CENTER));
} else if (d == 7) {
p3.add(new JLabel("2", JLabel.CENTER));
} else {
p3.add(new JTextField(1));
}
}
}
Solution
Declare a List
before the for
loop and change the else statement to
else {
JTextField tf = new JTextField(1);
list.add(tf);
p3.add(tf);
}
After that iterate over the list
and get the text from each element.