'Why does my for loop to disable buttons not work, but the same loop works for setting colors in JFrame?

I am programming a TicTacToe game and I want to use a for loop with my JButton components inside them like this:

private void clearField(){
    for (int i = 0; i<PlayField.length; i++){
        PlayField[i].setText("");
        PlayField[i].setBackground(new Color(41,200,193));
    }
}

Playfield in this example is an Array with all my nine buttons, that loop works but this one doesn't:

private void setInvis(){
    for (int i = 0; i<PlayField.length; i++){
        PlayField[i].setVisible(false);
    }
}


Solution 1:[1]

Like stated in a previous comment, JButton.setVisible(false); only changes the user's ability to see the component. In the eyes of the frame itself, the component is still there. If this is what you want, and it still doesn't work, you may have to call JFrame.revalidate(); after changing the state of the button. This method essentially 'refreshes' the frame whenever you add components or change them. See more here

If your goal is to make the buttons visible, but disable them, use JButton.setVisible(false);.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Adan