Home Answers Viewqa Java-Beginners using getText() to retrieve text from an array of text fields with loop.

 
 


akash purohit
using getText() to retrieve text from an array of text fields with loop.
2 Answer(s)      a year and 9 months ago
Posted in : Java Beginners

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 Pages:

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.