I have created chess board using JButton
and for pieces I used ASCII values.
I added ActionListener
to all the buttons. When i clicked source(first) button actionPerformed
event is called and i stored the value of the button in the variable.
But the problem is when i clicked the destination button actionPerformed
event is called and value is replaced with source button value.
I want source button value and destination button value in different variables. How it is possible?
public void actionPerformed(ActionEvent ae)
{
JButton o = (JButton) ae.getSource();
value = o.getText();
}
Then you need 2 variables to store the two values and a third variable to tell you which click is happening.
if(isSource){
source = o.getText();
isSource = false;
}else{
destination = o.getText();
isSource = true;
}
This way, on the first click you know the value of the source, and on teh second click you know the value of the destination. Then on the next click it is a source again etc.