-1

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()));;

                        }
                    }
                }

            }
        });

    }
  • `final TextField[][] fields = new TextField[9][9];` – Betlista Feb 06 '18 at 23:43
  • You elly shouldn't be using X and Y out of those two for loops... – Betlista Feb 06 '18 at 23:48
  • 1
    `frame.getContentPane().setLayout(null);` .. ***Why?*** In the [last question](https://stackoverflow.com/questions/48623643/how-to-make-gridbaglayout-specific-size) you were using layouts. Why toss all that functionality out the window to use your own (fragile) positioning and sizing logic? And if the logic becomes robust, why not wrap it in a custom layout manager? Note the screen shot in the last question could be achieved with just two layouts (and the 'squaring' the text fields fixed as shown in the answer). As an aside, why do only 2 of your 9 questions have an **accepted** answer? – Andrew Thompson Feb 06 '18 at 23:54
  • `if (fields[1][1].getText().isEmpty()) { System.out.println(field[X][Y] + " is empty");` --- If you're specifically testing `1,1` for empty text, why is print statement trying to use `X,Y`? Why is `X,Y` even declared standalone, and not as part of the `for` loops? --- Unrelated, why are you mixing `< 9` and `<= 8` in the `for` loops. Consistent coding improves code clarity. Standard loop logic suggest to use `< 9` in both cases. – Andreas Feb 07 '18 at 00:04
  • *"I'm getting an error saying that the variable must be final"* --- **The** variable? Which variable would that be? The error message would indicate which one it is. Your IDE would mark the code. We shouldn't need to guess which one it is. Seems you think it's the `fields` variable, when it's more likely that it's complaining about the `X` and `Y` variables. Please always include the exact error message in the question. – Andreas Feb 07 '18 at 00:08
  • Unable to reproduce error message. Copy/pasting your code, I get "`field` cannot be resolved to a variable". which is because it's supposed to be named `fields`. Please read: [How to create a Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve). – Andreas Feb 07 '18 at 00:13
  • @Andreas You're right, the problem was with the X and Y integers not the fields. If you would like to post your answer as reply ill mark it as the solution – Lachlan Langmead Feb 07 '18 at 02:01
  • @LachlanLangmead This is a simple coding error on your part, and is not helpful to others. Just delete the question. – Andreas Feb 07 '18 at 16:56

1 Answers1

-1

You can 'fix' it by making X and Y a final array with 2 elements. The array itself is final but you can always change the elements inside it.

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);

    final int fix[] = {0,0};
    fix[0] = 0;
    fix[1] = 0;
    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 (fix[1] = 0; fix[1] < 9; fix[1]++) {
            XPosition = 0;
            for (fix[0] = 0; fix[0] <= 8; fix[0]++) {
                fields[fix[0]][fix[1]] = new TextField(1);
                fields[fix[0]][fix[1]].setColumns(1);
                fields[fix[0]][fix[1]].setBounds(XPosition, YPosition, 32, 32);
                frame.getContentPane().add(fields[fix[0]][fix[1]]);

                XPosition = XPosition + 32;
            }
            YPosition = YPosition + 32;
        }
    }

    btnNewButton.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {

            if (fields[1][1].getText().isEmpty()) {

                System.out.println(fields[fix[0]][fix[1]] + " 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()));;

                    }
                }
            }

        }
    });

}
bhazero025
  • 337
  • 4
  • 15
  • 1
    `X` and `Y` will always be `9` by the time the event listener is called, which would then cause `ArrayIndexOutOfBoundsException`. Circumventing the `final` issue will only make the bad code worse. The solution is to not using the outside `X` and `Y` inside the even listener. – Andreas Feb 07 '18 at 00:17