I'm trying to create a program that creates a 9x9 array of text fields, then when a button is pressed, all the text fields with numbers in it are printed, and all the empty text fields print the error "text field is empty". However, I'm getting an error saying that the variable must be final. I'm assuming that this that the program assumes the the array "fields" is not yet set in stone.
public static void initialize() {
JFrame frame = new JFrame();
frame.setSize(316, 400);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JButton btnNewButton = new JButton("New button");
frame.getContentPane().add(btnNewButton);
btnNewButton.setBounds(1, 320, 310, 35);
int X;
int Y;
int XPosition = 21; // x location of textField
int YPosition = 21; // y location of textField
int[][] squares = new int[9][9];
TextField[][] fields = new TextField[9][9];
{
for (Y = 0; Y < 9; Y++) {
XPosition = 0;
for (X = 0; X <= 8; X++) {
fields[X][Y] = new TextField(1);
fields[X][Y].setColumns(1);
fields[X][Y].setBounds(XPosition, YPosition, 32, 32);
frame.getContentPane().add(fields[X][Y]);
XPosition = XPosition + 32;
}
YPosition = YPosition + 32;
}
}
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (fields[1][1].getText().isEmpty()) {
System.out.println(field[X][Y] + " is empty");
} else {
for (int Y = 0; Y < 9; Y++) {
for (int X = 0; X <= 8; X++) {
System.out.println(fields[1][1].getText());
squares[X][Y] = Integer.parseInt((fields[X][Y].getText()));;
}
}
}
}
});
}