0

I am creating a hangman game that uses a virtual keyboard for input. I created the keyboard layout using a for loop to create the buttons which then get put into a JButton ArrayList. All of the buttons make use of one ActionListener.

At the end of the game I need to reset all the buttons so they no longer have a border (A green or red border is placed around them during the game if the player guesses correctly). I have tried to loop through the JButton ArrayList to change the border but it only does it for the last button that was pressed. Is there a way that I can do this?

I also would like to know how to get the button back to it's original design, as when I use button.setBorder(null) it makes the button basically disappear. I would like to remove the MatteBorder I set earlier in the program.

public void createKeyboard() {

    String buttonLabel;
    char buttonLabelChar;

    for (char c = 'A'; c <= 'Z'; c ++) {    
        buttonLabelChar = c;
        buttonLabel = "" + buttonLabelChar; 
        JButton button = new JButton(buttonLabel);

        keyboardArray = new ArrayList<JButton>();
        keyboardArray.add(button);

        button.setPreferredSize(new Dimension(40, 40));
        pnlKeyboard.add(button);
        button.addActionListener(
            new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent event) {


                    hm.setLetterGuessed(event.getActionCommand().charAt(0));
                    hm.revealLetter();

                    if (hm.revealLetter()) {

                        button.setBorder(new MatteBorder(4, 4, 4, 4, Color.GREEN));
                        button.setEnabled(false);

                    }
                    else {

                        button.setBorder(new MatteBorder(4, 4, 4, 4, Color.RED));
                        button.setEnabled(false);
                        hm.loseLife();

                        setImage();
                    }

                    if (hm.winCheck()) {

                        JOptionPane.showMessageDialog(mainFrame, "You win!", "Game Over",
                                JOptionPane.INFORMATION_MESSAGE, new ImageIcon(Hangman.class.getResource("images/11rightleg.png")));

                        for (int i = 0; i < keyboardArray.size(); i ++) { // THIS DOESN'T WORK
                            button.setBorder(null);
                        }

                    }
                    else if (hm.getLives() == 0) {

                        JOptionPane.showMessageDialog(mainFrame, "You lose :(", "Game Over",
                                JOptionPane.INFORMATION_MESSAGE, new ImageIcon(Hangman.class.getResource("images/11rightleg.png")));

                    }


                    setWord();


                }
            }
        );

    }
}
jb2003
  • 123
  • 1
  • 11
  • 1
    1) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 2) A single blank line of white space in source code is all that is *ever* needed. Blank lines after `{` or before `}` are also typically redundant. 3) See [Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?](http://stackoverflow.com/q/7229226/418556) (Yes.) – Andrew Thompson Nov 29 '17 at 01:43

2 Answers2

0

One option is to avoid the anonymous class altogether, so that the listener can use the keyboardArray outside the createKeyboard() method:

ArrayList<JButton> keyboardArray;
public void createKeyboard() {

    String buttonLabel;
    char buttonLabelChar;

    for (char c = 'A'; c <= 'Z'; c ++) {    
        // ...

        keyboardArray = new ArrayList<JButton>();
        keyboardArray.add(button);

        // ...

        button.addActionListener(new ButtonListener());
    }
}

class ButtonListener {
    public void actionPerformed(ActionEvent event) {

        hm.setLetterGuessed(event.getActionCommand().charAt(0));
        hm.revealLetter();

        if (hm.revealLetter()) {
            button.setBorder(new MatteBorder(4, 4, 4, 4, Color.GREEN));
            button.setEnabled(false);
        } else {
            button.setBorder(new MatteBorder(4, 4, 4, 4, Color.RED));
            button.setEnabled(false);
            hm.loseLife();
            setImage();
        }

        if (hm.winCheck()) {
            JOptionPane.showMessageDialog(mainFrame, "You win!", "Game Over", JOptionPane.INFORMATION_MESSAGE, new ImageIcon( Hangman.class.getResource("images/11rightleg.png")));

            for (int i = 0; i < keyboardArray.size(); i ++) { // THIS DOESN'T WORK
                keyboardArray.get(i).setBorder(null);
            }
        } else if (hm.getLives() == 0) {
            JOptionPane.showMessageDialog(mainFrame, "You lose :(", "Game Over",
                            JOptionPane.INFORMATION_MESSAGE, new ImageIcon(Hangman.class.getResource("images/11rightleg.png")));
        }
        setWord();
    }
}
APerson
  • 8,140
  • 8
  • 35
  • 49
0

I found the problem. I had to create the keyboardArray ArrayList object inside the constructor.

jb2003
  • 123
  • 1
  • 11