Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



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

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


akash purohit
using getText() to retrieve text from an array of text fields with loop.
2 Answer(s)      3 years and 4 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.


Advertisement
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:
Advertisements
 

 

 

DMCA.com