using getText() to retrieve text from an array of text fields with loop.

using getText() to retrieve text from an array of text fields with loop.

hi, I have an array of about 50 textfields which show up when somebody presses button. Now I want to get the text from all the textfields using array....this is impossible the other way becouse then the program will become rigid....so I want to know how can I use getText() with array. eg.

for(int i=0;i<30;i++) { value[i]=text[i].getText(); }

//here value is an array. //text is the object(array) of textfield.

View Answers

August 24, 2011 at 11:08 AM

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

class MultipleTextboxes 
{
    public static void main(String[] args) 
    {
        JFrame f=new JFrame();
        JPanel p=new JPanel(new GridLayout(51,1));
        final JTextField text[]=new JTextField[50];
        JButton b=new JButton("Show Text");
        p.add(b);
        for(int i=0;i<text.length;i++){
            text[i]=new JTextField(20);
            p.add(text[i]);
        }
        b.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
            String values[]=new String[50];
            for(int i=0;i<values.length;i++){
                values[i]=text[i].getText();
            }
            for(int i=0;i<values.length;i++){
                System.out.println(values[i]);
            }
            }
        });
        f.add(p);
        f.setVisible(true);
        f.pack();
    }
}

August 24, 2011 at 10:10 PM

thanks for the code....but it won't work for getSelectedItem() in regards to the comboboxes. it says









Related Tutorials/Questions & Answers:

Ads