[Solved] Final local variable error


There are two problems. One, you cannot modify final variables. Two, you have to use final variables to have scope within the listeners. This code needs serious refactoring. However, here is a quick solution that requires minimal intervention.

  1. Remove all of the boolean tleft; boolean tmid; ... variables

  2. Add this enum outside of the main() method definition

    enum ESide {
      tleft, tmid, tright,
      mleft, mmid, mright,
      bleft, bmid, bright,
    

    };

  3. Add this definition at the appropriate spot

    // checker for if the button already has a character
    final Map<ESide, Boolean> board = new HashMap<>();
    for (ESide side : ESide.values()) {
        board.put(side, false);
    }
    
  4. Replace the listeners with the following, adjusting the tleft as appropriate. However, seriously consider making a method that returns the mouse adapter.

        public void mouseClicked(java.awt.event.MouseEvent evt)
        {
            if (board.get(ESide.tleft).booleanValue() == false) {
                topLeft.setText("X");
                board.put(ESide.tleft, true);
            }
        }
    

The error you are receiving will be eliminated.

2

solved Final local variable error