wood burning stoves 2.0*
The moose likes Swing / AWT / SWT and the fly likes problem with Action listener? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "problem with Action listener?" Watch "problem with Action listener?" New topic

problem with Action listener?

Melvin Guntanis

Joined: Jun 20, 2013
Posts: 1
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.

here are the methods
Maxim Karvonen
Ranch Hand

Joined: Jun 14, 2013
Posts: 101
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).

You should:
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.
Piet Souris
Ranch Hand

Joined: Mar 08, 2009
Posts: 419
Questions about chess are quite frequent on this forum. To get some ideas how you can implement Maxims
advises, search this forum for the word "chess", and you will find many ways that are helpful.
subject: problem with Action listener?
Similar Threads
create a jtable which can combine and split and the image change also
ui manager getColor and link JOptionpane OK button to code
How to make an 'expandable' button-like component
Cannot click the JButton in Jtable