I am getting multiple events when clicking my radiobutton

  • Follow


Hi

I have an application developed according to MVC. The models
implements one or more interfaces and invokes the listeners (views)
when updated. In one listener method that is invoked by an updated
model I have code that removes all radiobuttons kept in Vectors and
then creates a new bunch of radiobuttons in the Vectors.

It also does a

addMouseListener(new MouseAdapter() {
   public void mouseClicked(mouseEvent evt){
       System.out.println("I was clicked");
   }
});

to add eventhandling to be executed when the user clicks a
radiobutton

My problem is that when executing my application and clicking a
radiobutton then the mouseClicked method is executed 6 times!! When I
set a breakpoint at the System.out.println("I was clicked"); line then
I can see when execution reaches that line that I have 6
AWTEventMulticaster.mouseClicked(MouseEvent) invokations on the call
stack.
The row above the addMouseListener row is a
System.out.println("Entering..."); statement
When I choose to continue execution the application stops on the
breakpoint 6 times.
"Entering..." is printed only once. But "I was clicked" is printed 6
times.

I searched my project and I dont explicitly use AWTEventMulticaster
anywhere in my code.

I guess I have multiple threads somehow and from what I have read,
Swing is not threadsafe.
Does someone know how I can get only one invocation of mouseClicked
per clicked radiobutton?

thanx

0
Reply marcussilfver (26) 4/8/2008 7:30:35 PM

marcussilfver@gmail.com wrote:

> I guess I have multiple threads somehow and from what I have read,
> Swing is not threadsafe.

Thread safety is probably not the problem.  I think the addListner() 
method is safe. (I'd have to check that to be sure.)

> Does someone know how I can get only one invocation of mouseClicked
> per clicked radiobutton?

Can you post a short complete example that exhibits the same behavior? 
Probably it's just an error in your code but we'd have to actually look 
at it to find it.

Curious: how many radio buttons in this vector anyway?
0
Reply markspace (954) 4/9/2008 12:19:36 AM


marcussilfver@gmail.com wrote:

> My problem is that when executing my application and clicking a
> radiobutton then the mouseClicked method is executed 6 times!! When I

Note first that your code snippet above won't even compile.  MouseEvent 
starts with a capital M.

I just tried my own little code with four buttons in an ArrayList.  It 
prints "I was clicked" once for each button like is should.

I think you should put your debugger on the method that adds MouseEvents 
to the buttons and check that.  You're likely adding 6 handlers to each 
button.
0
Reply markspace (954) 4/9/2008 12:58:30 AM

On 9 Apr, 02:19, Mark Space <marksp...@sbc.global.net> wrote:
> marcussilf...@gmail.com wrote:
> > I guess I have multiple threads somehow and from what I have read,
> > Swing is not threadsafe.
>
> Thread safety is probably not the problem. =A0I think the addListner()
> method is safe. (I'd have to check that to be sure.)
>
> > Does someone know how I can get only one invocation of mouseClicked
> > per clicked radiobutton?
>
> Can you post a short complete example that exhibits the same behavior?
> Probably it's just an error in your code but we'd have to actually look
> at it to find it.
>

That will be hard to extract, it a rather complex application

> Curious: how many radio buttons in this vector anyway?

About 15-20 (depending on the model firing the updates)
0
Reply marcussilfver (26) 4/9/2008 5:07:48 AM

On 9 Apr, 02:58, Mark Space <marksp...@sbc.global.net> wrote:
> marcussilf...@gmail.com wrote:
> > My problem is that when executing my application and clicking a
> > radiobutton then the mouseClicked method is executed 6 times!! When I
>
> Note first that your code snippet above won't even compile. =A0MouseEvent
> starts with a capital M.
>

I did not cut'n paste the code sample, thats just a typo in my post,
real code is with capital M.

> I just tried my own little code with four buttons in an ArrayList. =A0It
> prints "I was clicked" once for each button like is should.
>

I have made a short snippet with 10 radiobuttons that when looking at
the code appears to be doing the same thing in the same order as the
major application. This little snippet behaves correctly and prints "I
was clicked" once for each button. That is why I figured that my
problem arise from putting the creation & eventhanling of the
radiobuttons in the method that the model fires update invocations
at.

> I think you should put your debugger on the method that adds MouseEvents
> to the buttons and check that. =A0You're likely adding 6 handlers to each
> button.

You mean the addMouseListener(new MouseAdapter() code line? Sure I
will give it a try!

0
Reply marcussilfver (26) 4/9/2008 5:19:26 AM

marcussilfver@gmail.com wrote:

> That is why I figured that my
> problem arise from putting the creation & eventhanling of the
> radiobuttons in the method that the model fires update invocations
> at.

I read this three times, I couldn't parse this sentence.

My little test had two classes.  One was the JFrame I used to test.  The 
other was pretty much just boilerplate Main class to start the app.

I add four MouseListeners, one for each button, in Main:

     private void testIt()
     {
         for ( int i = 0; i < 4; i++ )
         {
             final int j = i+1;
             rb.addButtonListener( i, new MouseAdapter()
             {
                 @Override
                 public void mouseClicked( MouseEvent e )
                 {
                     System.out.println( "I was clicked: "+j );
                 }
             });
         }
     }

(The JFrame object is stored in "rb" above.)

And in the JFrame class I just add the MouseListener to the requested 
button:

     public void addButtonListener( int which, MouseListener el )
     {
         JRadioButton r = buttons.get( which );
         r.addMouseListener( el );
     }

"buttons" is an ArrayList, not a Vector, but same idea.

I'd post the whole thing, but I made the JFrame in Matisse and it's a 
bit verbose, and it sounds like you've duplicated this example anyway. 
I'd put a breakpoint on your equivalent of the addButtonListener method 
above and watch for any button getting more than one MouseListener.
0
Reply markspace (954) 4/9/2008 7:55:11 AM

OK I have solved it. I thought I was clearing the Vectors before
creating radiobuttons, but I was not. I only cleared the Vector
containing ButtonGroup:s. So I did actually add multiple
mouselisteners to each one of the radiobuttons.

thanks
/Marcus
0
Reply marcussilfver (26) 4/10/2008 5:19:09 AM

marcussilfver@gmail.com wrote:
> OK I have solved it. I thought I was clearing the Vectors before
> creating radiobuttons, but I was not. I only cleared the Vector
> containing ButtonGroup:s. So I did actually add multiple
> mouselisteners to each one of the radiobuttons.

Are you using java.util.Vector?  Really?  Hadn't you better reconsider that?

-- 
Lew
0
Reply lew (2143) 4/10/2008 11:55:23 AM

7 Replies
28 Views

(page loaded in 0.111 seconds)


Reply: