how do i make the number of my textfileds dynamic? the number of my textfield depends on the parameter passed through the method setDetailsPanel(int s). the variable s determines the number of textfield i need.

i'm having this type of error. i don't seem to get the reason why.

D:\Program Files\Xinox Software\JCreator LE\MyProjects\Admin\Admin.java:563: incompatible types
found   : javax.swing.JTextField[]
required: javax.swing.JTextField
            txtSname[i] = new JTextField[s];
                                  ^

THE CODE

public JPanel setDetailsPanel(int s) {
    JTextField[] txtSname; 
    JTextField[] txtSage; 
    JTextField[] txtSschool; 

             sBtn.setVisible(false);
    JPanel detailsPanel = new JPanel(new BorderLayout());   
    JPanel labelPanel =  new JPanel();
    JLabel lblSname = new JLabel("Name", JLabel.LEFT);
    JLabel lblSage = new JLabel("Age", JLabel.CENTER);
    JLabel lblSschool = new JLabel("School", JLabel.RIGHT);
             labelPanel.add(lblSname);
    labelPanel.add(lblSage);
             labelPanel.add(lblSschool);
    detailsPanel.add(labelPanel, BorderLayout.NORTH);

            int i=1;
    JPanel tp;
    JPanel textFields = new JPanel(new GridLayout(s,1));

    while (i<=s) {
        tp = new JPanel();
        txtSname[i] = new JTextField[s];
        txtSage[i] = new JTextField[s];
        txtSschool[i] = new JTextField[s];
        tp.add(txtSname[i]);
        tp.add(txtSage[i]);
        tp.add(txtSschool[i]);
        textFields.add(tp);
        i++;
    }

    detailsPanel.add(textFields, BorderLayout.CENTER);

    return detailsPanel;
}

GUYS, PLEASE DON'T MIND MY QUESTION. MINUTES AFTER I POST THIS QUESTION I REVIEWED MY CODE AND FOUND OUT WHERE I WAS DOING WRONG. I'VE BEEN PONDERING ON WHAT TO DO WITH THIS DYNAMIC THING BEFORE I RESULT INTO AN ARRAY OF TEXTFIELDS. AND IT TOOK ME SO LONG TO FIGURE OUT HOW AND I'VE BEEN SO FRUSTRATED ON FINDING THE ANSWER. ANYWAY, I HOPE YOU COULD HELP ME IF EVER I COME ACROSS WITH ANOTHER PROBLEM.

THE CODE

public JPanel setDetailsPanel(int s) {
     sBtn.setVisible(false);
    JPanel detailsPanel = new JPanel(new BorderLayout());   
    JPanel labelPanel =  new JPanel();
    JLabel lblSname = new JLabel("Name", JLabel.LEFT);
    JLabel lblSage = new JLabel("Age", JLabel.CENTER);
    JLabel lblSschool = new JLabel("School", JLabel.RIGHT);
             labelPanel.add(lblSname);
    labelPanel.add(lblSage);
             labelPanel.add(lblSschool);
    detailsPanel.add(labelPanel, BorderLayout.NORTH);

             int i=1;
    JPanel tp;
    JPanel textFields = new JPanel(new GridLayout(s,1));
JTextField[] txtSname = new JTextField[s]; 
    JTextField[] txtSage = new JTextField[s]; 
    JTextField[] txtSschoo = new JTextField[s]l; 


    while (i<=s) {
        tp = new JPanel();
        txtSname[i] = new JTextField(15);
        txtSage[i] = new JTextField(2);
        txtSschool[i] = new JTextField(2);
        tp.add(txtSname[i]);
        tp.add(txtSage[i]);
        tp.add(txtSschool[i]);
        textFields.add(tp);
        i++;
    }

    detailsPanel.add(textFields, BorderLayout.CENTER);

    return detailsPanel;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.