Does any one know of a class that creates unlimited JTextFields that can collect data?
Thanks.

Recommended Answers

All 9 Replies

I have this class but in java is txt actually a new JTextField() if the name is changed?
this creates a form from any JTable Model.
Is it possible to get unique txt.getText(); from txt fields if the txt.setName() is unique?

public void buildForm(){
        float offSetX=0;
        float offSetY=10;
        int x=1;
        int count=0;
        thisTxtFieldArray=new String[numberOfColumns];
        for(String s:columnNameList){
            //displayPanel.setLayout(new FlowLayout());
            JPanel p =new JPanel();
            p.setBorder(BorderFactory.createEtchedBorder());
            p.setLayout(new GridLayout(1,0,1,0));
            p.setAlignmentX(offSetX);
            p.setAlignmentY(offSetY);
            offSetY++;
            p.setPreferredSize(new Dimension(20,500));
            displayPanel.add(p);
            lbl=new JLabel();
            lbl.setPreferredSize(new Dimension(40,20));
            lbl.setText(s);
            p.add(lbl);
            txt =new JTextField();
            txt.setName("txt"+s);
           
            System.out.println("String txt"+x+"="+"thisTxt"+x+";");
            txt.setPreferredSize(new Dimension(20,120));
            lbl.setLabelFor(txt);
            p.add(txt);
            displayPanel.add(p);
            x++;
            count++;
        }
    }

is txt actually a new JTextField() if the name is changed

No. The name belongs to the Component class a parent of the textfield class. The textfield object is the same object created with the new statement.

Is it possible to get unique txt.getText(); from txt fields if the txt.setName() is unique

Don't understand this question. The name value stored in the object doesn't change the rest of the object.

I am just trying to understand why that code shown makes new labels with the (unique) text
for every column in the in the table and it is paired with a JTextField and the display
shows all the new display objects just like a form.
I was trying to find out if this code could collect the txtfield.getText().

It just makes a new object on the screen for any number of columns but how is one different from the other so I can always get the txt for each.

I just confused because in flash (actionSript) you can create on object and use it 1500
different ways and you can change the name for each and then manipulate any given object by the name related to it.

how is one different from the other so I can always get the txt for each.

You can get to each textfield/object by saving a reference to it. Usually there is a variable name associated with each object. But in some cases you store the references to an object in some "container" like an array or Map. Then to get to the object you need to get the reference from that "container".

TextField tf = new TextField(); // create variable (tf) and an object
tf now refers to a textfield

TextField[] tfArray = new TextField[22]; // create an array to hold references
tfArray[0] = tf; // save reference to object created above
tf = null; //now tf does NOT refer/point to the object created above
...
tf = tfArray[0]; // now tf again refers/points to the object created above

I'm like-in that. thanks.
I have not ever bothered to actually use a array[][] for java.
I have been trying to comprehend if your example would be better to use a array[][]. txtGetNameArray=new String[numberOfColumns][2] Because at the actionPerformed() would it be possible to load a reference to all the txt fields and the getText() they hold. I will have to use your example first to know if that is even necessary.
thanks

What do you need access to in the actionPerformed() method?
If references to all your text fields are in an array, then you can get the text from them by: String text = anArrayOfTF[ix].getText();

Thanks,your example will save a lot of time. Before your example I was trying to find a excuse to actually use a multi-dementional array. I have never really wrapped my brain around how it works.
I just started learnng the art of JTable. Isn't that all a JTable is at its root?

This does the trick. thanks again

public void buildForm(){
        float offSetX=0;
        float offSetY=10;
        int x=1;
        int count=0;
        txtFieldArray=new JTextField[columnNameList.size()];
        for(String s:columnNameList){
            JPanel p =new JPanel();
            p.setBorder(BorderFactory.createEtchedBorder());
            p.setLayout(new GridLayout(1,0,1,0));
            p.setAlignmentX(offSetX);
            p.setAlignmentY(offSetY);
            offSetY++;
            p.setPreferredSize(new Dimension(20,500));
            displayPanel.add(p);
            JLabel lbl=new JLabel();
            lbl.setPreferredSize(new Dimension(40,20));
            lbl.setText(s);
            p.add(lbl);
            JTextField txt =new JTextField();
            txt.setName("txt"+s);           
            System.out.println("String txt"+x+"="+"thisTxt"+x+";");
            txt.setPreferredSize(new Dimension(20,120));
            txtFieldArray[x-1]=txt;           
            lbl.setLabelFor(txt);
            p.add(txt);
            displayPanel.add(p);
             txt=null;
            x++;
            count++;
        }
    }
    
   
    @Override
    public void actionPerformed(ActionEvent evt) {

        if (evt.getSource() == submitInsertBtn) {
            for(int i=0;i<columnNameList.size();i++){
                System.out.println("Collect this data: "+txtFieldArray[i].getText());
            }
        }
            
            
    }
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.