I am getting multiple events when clicking my radiobutton

M

marcussilfver

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
 
M

Mark Space

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?
 
M

Mark Space

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.
 
M

marcussilfver

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


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)
 
M

marcussilfver

Note first that your code snippet above won't even compile.  MouseEvent
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.  It
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.  You're likely adding 6 handlers to each
button.

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

Mark Space

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.
 
M

marcussilfver

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,999
Messages
2,570,243
Members
46,836
Latest member
login dogas

Latest Threads

Top