4 Replies - 415 Views - Last Post: 21 June 2013 - 03:53 AM Rate Topic: -----

#1 guntanis  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 9
  • Joined: 20-June 13

action listener, how can i change it so it runs just once?

Posted 20 June 2013 - 06:37 PM

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
 public void caballo(final int row, final int column) {
 
        final JButton current = mesa[row][column];
 
        current.setIcon(image);
        panel.repaint();
 
        acciones(row, column, current);
    }
 
    public void acciones(final int row, final int column, final JButton current) {
 
        for (int i = 0; i < HEIGHT; i++) {
            for (int j = 0; j < WIDTH; j++) {
                mesa[i][j].addActionListener(e(row, column, current));
 
 
 
            }
        }
    }
 
    public ActionListener e(final int row, final int column,
            final JButton current) {
        return new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                quitarAccion(row, column, current);
                if (tienebotton(row + 2, column + 1)) {
                    if (e.getSource() == mesa[row + 2][column + 1]) {
 
                        current.setIcon(null);
                        caballo(row + 2, column + 1);
                        ((AbstractButton) e.getSource()).setEnabled(false);
 
                    }
                }
                if (tienebotton(row + 2, column - 1)) {
                    if (e.getSource() == mesa[row + 2][column - 1]) {
 
                        current.setIcon(null);
                        caballo(row + 2, column - 1);
 
                        ((AbstractButton) e.getSource()).setEnabled(false);
 
                    }
                }
                if (tienebotton(row - 2, column - 1)) {
                    if (e.getSource() == mesa[row - 2][column - 1]) {
 
                        current.setIcon(null);
                        caballo(row - 2, column - 1);
 
                        ((AbstractButton) e.getSource()).setEnabled(false);
 
                    }
                }
                if (tienebotton(row - 2, column + 1)) {
                    if (e.getSource() == mesa[row - 2][column + 1]) {
 
                        current.setIcon(null);
                        caballo(row - 2, column + 1);
 
                        ((AbstractButton) e.getSource()).setEnabled(false);
 
                    }
                }
 
                if (tienebotton(row + 1, column + 2)) {
                    if (e.getSource() == mesa[row + 1][column + 2]) {
 
                        current.setIcon(null);
                        caballo(row + 1, column + 2);
 
                        ((AbstractButton) e.getSource()).setEnabled(false);
 
                    }
                }
                if (tienebotton(row - 1, column + 2)) {
                    if (e.getSource() == mesa[row - 1][column + 2]) {
 
                        current.setIcon(null);
                        caballo(row - 1, column + 2);
 
                        ((AbstractButton) e.getSource()).setEnabled(false);
 
                    }
                }
                if (tienebotton(row + 1, column - 2)) {
                    if (e.getSource() == mesa[row + 1][column - 2]) {
 
                        current.setIcon(null);
                        caballo(row + 1, column - 2);
 
                        ((AbstractButton) e.getSource()).setEnabled(false);
 
                    }
                }
                if (tienebotton(row - 1, column - 2)) {
                    if (e.getSource() == mesa[row - 1][column - 2]) {
 
                        current.setIcon(null);
                        caballo(row - 1, column - 2);
 
                        ((AbstractButton) e.getSource()).setEnabled(false);
 
                    }
                }
            }
        };
    }
 
    public boolean tienebotton(int row, int column) {
        return (row >= 0 && row < HEIGHT && column >= 0 && column < WIDTH);
 
    }
 
    public void quitarAccion(final int row, final int column, final JButton current) {
        for (int i = 0; i < HEIGHT; i++) {
            for (int j = 0; j < WIDTH; j++) {
                mesa[i][j].removeActionListener(e(row, column, current));
 
 
 
            }
        }
    }
}


Is This A Good Question/Topic? 0
  • +

Replies To: action listener, how can i change it so it runs just once?

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: action listener, how can i change it so it runs just once?

Posted 20 June 2013 - 07:28 PM

What an horror !!! :)
Don't create an ActionListener inside an actionPerformed() method
You can't return anything from an actionPerformed() so your code won't even compile

2 possible solutions:
- in the actionPerformed() disable the object that can trigger the ActionEvent
- use a count of the number of time it is called
Was This Post Helpful? 1
  • +
  • -

#3 guntanis  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 9
  • Joined: 20-June 13

Re: action listener, how can i change it so it runs just once?

Posted 20 June 2013 - 07:54 PM

View Postpbl, on 20 June 2013 - 07:28 PM, said:

What an horror !!! :)/>
Don't create an ActionListener inside an actionPerformed() method
You can't return anything from an actionPerformed() so your code won't even compile

2 possible solutions:
- in the actionPerformed() disable the object that can trigger the ActionEvent
- use a count of the number of time it is called

can you specify a bit more, im kind of new to java :/ i don't get the solucions. thanks
Was This Post Helpful? 0
  • +
  • -

#4 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1400
  • View blog
  • Posts: 3,108
  • Joined: 05-April 11

Re: action listener, how can i change it so it runs just once?

Posted 20 June 2013 - 11:41 PM

View Postpbl, on 21 June 2013 - 02:28 AM, said:

What an horror !!! :)/>/>
Don't create an ActionListener inside an actionPerformed() method
You can't return anything from an actionPerformed() so your code won't even compile

2 possible solutions:
- in the actionPerformed() disable the object that can trigger the ActionEvent
- use a count of the number of time it is called

If you are talking about the method named 'e' then it isn't an actionPerformed method
It is simply returning an ActionListener.. And I don't get the name 'e' either, what is that supposed to mean :D
public ActionListener e(final int row, final int column, final JButton current) {


This post has been edited by CasiOo: 20 June 2013 - 11:42 PM

Was This Post Helpful? 1
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: action listener, how can i change it so it runs just once?

Posted 21 June 2013 - 03:53 AM

Ha OK a method named "e" which plays with a variable named "e"
Why not making it more confusing ... and you shouls call e.getSource() only once
And return when done no need to perform all the if everytime. Something like:

   public ActionListener e(final int row, final int column, final JButton current) {
        return new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                quitarAccion(row, column, current);
                AbstractButton btn = (AbstractButton) e.getSource();  // <--- called once

                if (tienebotton(row + 2, column + 1)) {
                    if (btn == mesa[row + 2][column + 1]) {
 
                        current.setIcon(null);
                        caballo(row + 2, column + 1);
                        btn.setEnabled(false);
 
                    }
                    return;     // <--- done
                }
                if (tienebotton(row + 2, column - 1)) {
                    if (btn == mesa[row + 2][column - 1]) {
 
                        current.setIcon(null);
                        caballo(row + 2, column - 1);
 
                        btn.setEnabled(false);
 
                    }
                    return;
                }
...


Was This Post Helpful? 1
  • +
  • -

Page 1 of 1