> 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. 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!
Mark Space - 09 Apr 2008 08:55 GMT
> 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.
marcussilfver@gmail.com - 10 Apr 2008 06:19 GMT
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
Lew - 10 Apr 2008 12:55 GMT
> 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?

Signature
Lew