Pour récupérer les valeurs: il faut bien retenir les références aux JTextField quelque part pour pouvoir les accéder ultérieurement.
Un petit exemple fait à la va-vite:
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Nina2009Sample extends JFrame {
private static final long serialVersionUID = 1L;
private final static int ROW_SIZE = 4;
private JTextField[] tfArray = new JTextField[ROW_SIZE * ROW_SIZE];
public static void main(String args[]) {
try {
Nina2009Sample frame = new Nina2009Sample();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
public Nina2009Sample() {
super();
getContentPane().setLayout(new BorderLayout());
setBounds(100, 100, 500, 375);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JPanel centerPane = new JPanel();
centerPane.setLayout(new GridLayout(ROW_SIZE, ROW_SIZE));
getContentPane().add(centerPane);
final JPanel southPane = new JPanel();
final FlowLayout flowLayout_1 = new FlowLayout();
flowLayout_1.setAlignment(FlowLayout.RIGHT);
southPane.setLayout(flowLayout_1);
getContentPane().add(southPane, BorderLayout.SOUTH);
final JButton printCellValuesButton = new JButton();
printCellValuesButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
for (int i = 0; i < ROW_SIZE; i++) {
for (int j = 0; j < ROW_SIZE; j++) {
System.out.print("[" + tfArray[i * ROW_SIZE + j].getText() + "]\t");
}
System.out.println();
}
}
});
printCellValuesButton.setText("Print Cell Values");
southPane.add(printCellValuesButton);
final FlowLayout flowLayout = new FlowLayout();
flowLayout.setAlignment(FlowLayout.RIGHT);
for (int i = 0; i < ROW_SIZE; i++) {
for (int j = 0; j < ROW_SIZE; j++) {
tfArray[i * ROW_SIZE + j] = new JTextField("cell " + i + ":" + j);
centerPane.add(tfArray[i * ROW_SIZE + j]);
}
}
//
}
}
Voilà...
++