0

I have an array of JTextField and when a JTextField is modified I want to have the name of the JTextField and the text.

while ((line = bufferedReader.readLine()) != null) { // 1 by 1 line of file
    if (f == 6) {
        g++;
        f = 0;
    }
    tableauDonnee[g][f] = line;
    fields[g][f] = new JTextField(tableauDonnee[g][f]);
    fields[g][f].setName(String.valueOf(g + etf));
    fields[g][f].setBounds(positionY, positionX, 160, 40);
    pan.add(fields[g][f]);
    positionY = positionY + 180;
    f++;

I can do that before add JTextField to panel

fields[g][f].addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent event) {
        textfield = fields[g][f].getText();
        nameTextfield = fields[g][f].getName();
    }
});

But it will not work because values of g and f are not good.

I'm stuck and don't know what to do.

Mr. Polywhirl
  • 42,981
  • 12
  • 84
  • 132

1 Answers1

1

You can get the source of the event (ie, the text field where you pressed the Enter key) from the ActionEvent:

public void actionPerformed(ActionEvent event) 
{
    JTextField textField = (JTextField)event.getSource();
    ...
}

Also, why are you use a 2D array of text fields.

I would suggest a JTable would be the better components to use. Read the section from the Swing tutorial on How to Use Tables for more information.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • If i use it it return me that javax.swing.JTextField[5,750,110,160x40,layout=javax.swing.plaf.basic.BasicTextUI$UpdateHandler,alignmentX=0.0,alignmentY=0.0,border=javax.swing.plaf.BorderUIResource$CompoundBorderUIResource@4fa1d375,flags=296,maximumSize=,minimumSize=,preferredSize=,caretColor=sun.swing.PrintColorUIResource[r=51,g=51,b=51],disabledTextColor=javax.swing.plaf.ColorUIResource[r=184,g=207,b=229],editable=true,margin=javax.swing.plaf.InsetsUIResource[top=0,left=0,bottom=0,right=0],sel etc. Where can i retrieve the name and text. By the way textfield is a string not a jtextfield – lefebvre remy Dec 03 '19 at 19:18
  • Excuse me your a genius. I take textfield as jtexfield and after i retrieve name and text from textfield – lefebvre remy Dec 03 '19 at 19:21