Wasiq Noor
Wasiq Noor

Reputation: 13

Action Listener detects multiple Events for a Single Event

I have designed a panel that includes some buttons with it. Buttons are attached with an ActionListener. When ever i click on that buttons this ActionListener detects 4 events for this single click. Whereas it should detect only one. Does anybody know what exactly the reason is?

public class Buttons extends JPanel
{
private JButton undo=new JButton("Undo");
private JButton replay=new JButton("Replay");

public void paint(Graphics g)
{
    super.paint(g);
    super.setSize(new Dimension(560,30));
    super.add(replay);
    super.add(undo);
    undo.setBorder(new LineBorder(Color.WHITE,3));
    replay.setBorder(new LineBorder(Color.WHITE,3));

    undo.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent arg0) {
            // TODO Auto-generated method stub
            Controler.pieces.undo();
            Controler.reDraw();

        }
    });
    replay.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent arg0) {
            // TODO Auto-generated method stub
            System.out.println("Dastiii");
        }
    });

}
 }

and these events are being used here

public void undo()
{
    System.out.print(Controler.allMoves.size());
    if(Controler.allMoves.size()<=1)
    {
        init_board();
        return;
    }
    Piece temp[][]=Controler.allMoves.get(Controler.allMoves.size()-2);
    for(int i=0;i<8;i++)
    {
        for(int j=0;j<8;j++)
        {
            board[i][j].set_name(temp[i][j].get_name());
            board[i][j].set_oneWay(temp[i][j].get_oneWay());
        }
    }
    Controler.allMoves.remove(Controler.allMoves.size()-2);
}

Upvotes: 1

Views: 308

Answers (1)

MadProgrammer
MadProgrammer

Reputation: 347234

Your registering you ActionListeners within the paint method!!

Let's not even worry about the fact that it's un-recommended to override paint

Never change or modify the state of the component or any of it's child components within in any paint method, these will be called multiple times during the execution of your application. For example, it's not unusual for a paint method to be called 2-4 times just when the main window is made visible...

public void paint(Graphics g)
{
    super.paint(g);
    /** All this should be done within the constructor
    // If you are using a layout manager, this is pointless, if your not
    // then that's another problem
    super.setSize(new Dimension(560,30));
    super.add(replay);
    super.add(undo);
    undo.setBorder(new LineBorder(Color.WHITE,3));
    replay.setBorder(new LineBorder(Color.WHITE,3));

    undo.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent arg0) {
            // TODO Auto-generated method stub
            Controler.pieces.undo();
            Controler.reDraw();

        }
    });
    replay.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent arg0) {
            // TODO Auto-generated method stub
            System.out.println("Dastiii");
        }
    });
    **/

}

Take a look at:

For more details about how and what painting is in Swing

Upvotes: 3

Related Questions