Reputation: 19
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.
Upvotes: 0
Views: 117
Reputation: 324147
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.
Upvotes: 1