-->

get text from dynamically added jPanel

2020-02-07 10:26发布

问题:

I am adding dynamically subPanel to jPanel1 (with jTextField and jButton). Some of part code was borrowed from there.

I am trying to get text from components of jPanel1, but cannot succeed.

EDITED: This is a subPanel that contains jTextField, +Button and -Button.

private class subPanel extends javax.swing.JPanel {         
        subPanel me;
        public subPanel() {
            super();
            me = this;
            JTextField myLabel = new JTextField(15);
            add(myLabel);
            JButton myButtonRemove = new JButton("-");
            JButton myButtonAdd = new JButton("+");
            add(myButtonRemove);
            add(myButtonAdd);

Here is code of AddButton:

    jPanel1.add(new subPanel());
    pack();

The code that I am trying to get text from jTextField doesn't work:

     Component[] children = jPanel1.getComponents();
     for (int i=0;i<children.length;i++){
     if (children[i] instanceof JTextField){
     String text = ((JTextField)children[i]).getText();
     System.out.println(text);

}

Your response will be greatly appreciated.

回答1:

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);
            }
        }
    }
}


回答2:

I know this a bit old question to answer, but still it can potentially help more future viewers. My initial response was to write this as a comment in @Oliver Schmidt , but as I don't have 50 Reputation yet, so decided to write this in a separate answer.

To get access to the components contained in the dynamically added jPanel "subPanel", you don't necessarily need to use second for loop, you can access subPanel's components directly by using the following line of code, for example,

String s1 = ((subPanel) sp).jcb1.getSelectedItem().toString().substring(1, 2); ...

So the overall code looks like,

String s1;
String s2;
String s3;
String text;
Component[] c = jPanel1.getComponents();

for (Component sp : c) {
    if (sp instanceof subPanel) {

        //access the component from properly type converted subPanel
        s1 = ((subPanel) sp).jcb1.getSelectedItem().toString().substring(1, 2);
        s2 = ((subPanel) sp).jcb2.getSelectedItem().toString().substring(1, 2);
        s3 = ((subPanel) sp).jcb3.getSelectedItem().toString().substring(1, 2);
        text = ((subPanel) sp).myLabel.getText();
    }
}

System.out.println(s1);
System.out.println(s2);
System.out.println(s3);
System.out.println(text);