Results 1 to 3 of 3
  1. #1
    guntanis is offline Member
    Join Date
    Jun 2013
    Posts
    1
    Rep Power
    0

    Default problem with Action listening?

    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

    Java Code:
       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));
    
    
    
                }
            }
        }
    }

  2. #2
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default Re: problem with Action listening?


  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default Re: problem with Action listening?

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Replies: 7
    Last Post: 12-16-2012, 10:20 PM
  2. Action Listening/Handling Troubles.
    By gaeronf in forum AWT / Swing
    Replies: 4
    Last Post: 11-05-2012, 05:04 AM
  3. Problem with action performing !!
    By baig-sh in forum New To Java
    Replies: 52
    Last Post: 11-11-2011, 02:29 AM
  4. problem in action
    By jperson in forum New To Java
    Replies: 2
    Last Post: 07-24-2010, 01:00 PM
  5. Socket Listening problem in Netbeans
    By adeel109 in forum Networking
    Replies: 0
    Last Post: 05-06-2010, 03:21 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •