so I am making a chess game but only with the knight.my problem is that when I move the piece the first time a new knights appear were I could have move it before. So I was thinking maybe if i remove the actionlistener inside the action perform method i could fix this. but it didn't. Do any 1 know how can I rewrite the code without having to addActionlisteners every time I run it? Please help im desperate Here is the methods related to this. Any kind of help is welcome.
As I can see, you add listeners for each button to any other button. It is wrong. And you removeActionListener action does nothing, because you create new listener instances, which are not equal to any one added (they use reference identity in your case).
1. Attach one and only one listener for each button. That listener will listen for a button click and process a move when knight can go to a specified point. So, point 2.
2. Store knight's position in a variables. You can check when move is correct or not. Don't use event source (it always will be a button where knight should move) and you know coordinates of that button (you can pass them in a listener creation function). Use knight position variables. And after a move update the variables first and only then update button states/icons.