user3458412
user3458412

Reputation: 1

Java ActionListener (multiple statements)

The following is the edited and (extremely basic version) of my codes so you could understand clearly what it is that i am trying to do. I would like to use:

public void actionPerformed(ActionEvent e){ }    

The problem is that i can't edit/ manupulate moves for my chess pieces in the loops under ActionListener but it never functions all statement, it always only functions on one if statement and ignores the others.

if(e.getSource()==cSquare[a][b]){  //current source
    if(switcher=="1" && cSquare[a][b].getChessPiece()=="Pawn" ) {}

Simple example of ActionListener:

 for(int i=0;i<8;i++){
        for(int j=0;j<8;j++){  

....

 elements[i][x] = new OriginalElements(i, x, "None");
                elemenents[i][j].setIcon(None);   

.......

        if(elements[i][x].Xpos()==2 && elements[i][x].Ypos()==2){
                        elements[i][x].setIcon(Image);
                        cSquare[i][x].setChessPiece("Image");
                    }
                    elements[i][j].addActionListener(cListener);   //Click do this
                    panel.add(elements[i][jx);  

My question would be; how could i implement multiple (if, else etc) statements into the ActionListener and make it work on all? rather than only one. I've tried every possibility i know but only seems to work on a single element (e.g. move image from square to another square and replace current square image with 'EmptyImage'.
Thank you for your time.

P.S : ignore the "professonality" of the codes. I changed it to give you a clear understanding;; thanks :)

Upvotes: 0

Views: 103

Answers (1)

npinti
npinti

Reputation: 52185

You have various errors in your code:

if(e.getSource()==cSquare[a][b]){  //current source
    if(switcher=="1" && cSquare[a][b].getChessPiece()=="Pawn" ) {}

Strings are compared with the .equals(String str) method.

 if(elements[i][x].Xpos()==2 && elements[i][x].Ypos()==2){
                        elements[i][x].setIcon(Image);
                        cSquare[i][x].setChessPiece("Image");

If statements do not end with a semi colon (;).

Fix those and see how it goes.

Upvotes: 2

Related Questions