sjohnson
sjohnson

Reputation: 69

Java MouseEvent left click inside actionPerformed

I have a MouseListener inside my action, the action corresponds to a JButton. The mouse event works fine for right click events, but for left click events It seems not be resetting my mouse events so every left click plays through every left click mouse event that was before it. So the first left click will increment my number by 1, the second increments it by 2 the third by 3 the fourth 4 etc. It should only increment by 1 every time. I did some debugging and it seems that the left click function is called for every left click on the history of the component.

Any thoughts?

private void setupActions()
{
    oneAction = new AbstractAction( "1" )
    {
       @Override
        public void actionPerformed( ActionEvent e )
        { 

          ((JButton)e.getSource()).addMouseListener(new MouseAdapter() 
            { 
                @Override
                public void mouseClicked( MouseEvent e )
                {
                   if ( SwingUtilities.isLeftMouseButton(e) )
                    {
                        Integer quantityField = (Integer) model.getValueAt(0, 3);
                        if ( quantityField == null || quantityField == 0)
                        {
                            quickOrderTableModel.setValueAt(1, 0, 3);
                        }
                        else 
                        {
                            Integer newValue = (quantityField + 1);
                            quickOrderTableModel.setValueAt(newValue, 0, 3);
                        }

                    }
                    if ( SwingUtilities.isRightMouseButton(e) )
                    {

                        Integer quantityField = (Integer) model.getValueAt(0, 3);
                        if ( quantityField == null || quantityField == 0)
                        {
                            quickOrderTableModel.setValueAt(0, 0, 3);     
                        }  
                        else
                        {
                            Integer newValue = (quantityField - 1);
                            quickOrderTableModel.setValueAt(newValue, 0, 3); 
                        }

                    }
                }

            });
        }

    };

Upvotes: 0

Views: 3517

Answers (1)

dic19
dic19

Reputation: 17971

I did some debugging and it seems that the left click function is called for every left click on the history of the component.

The problem is you add a new MouseListener every time the button is pressed:

@Override
public void actionPerformed( ActionEvent e ) {
    ((JButton)e.getSource()).addMouseListener(new MouseAdapter() {...});
}

Consequently you'll have a call for every listener attached to the button.

On the other hand if the goal is increase/decrease some count variable based on what mouse button is pressed, then you don't need an ActionListener at all: just add the mouse listener once and that's it:

JButton button = new JButton("1");
button.addMouseListener(new MouseAdapter() {...});

However IMO the best approach would be having two buttons, implement two different actions and forget about mouse listeners.

Upvotes: 1

Related Questions