I am adding dynamically subPanel to jPanel1 (with jTextField and jButton). Some of part code was borrowed from there.
Problem is: You are iterating over the children of jPanel1:
jPanel1.getComponents();
And expect there to be a JTextField:
if (children[i] instanceof JTextField){
String text = ((JTextField)children[i]).getText();
System.out.println(text);
}
But since you have added subPanels to jPanel1, the children of jPanel1 are subPanels, not JTextFields!
So, in order to access the JTextFields, you'll have to iterate over the children of the subPanels in a second for-loop!
Example:
Component[] children = jPanel1.getComponents();
// iterate over all subPanels...
for (Component sp : children) {
if (sp instanceof subPanel) {
Component[] spChildren = ((subPanel)sp).getComponents();
// now iterate over all JTextFields...
for (Component spChild : spChildren) {
if (spChild instanceof JTextField) {
String text = ((JTextField)spChild).getText();
System.out.println(text);
}
}
}
}