I am getting multiple events when clicking my radiobutton

Discussion in 'Java' started by marcussilfver, Apr 8, 2008.

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

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

    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?

    marcussilfver, Apr 8, 2008
    1. Advertisements

  2. marcussilfver

    Mark Space Guest

    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.

    Curious: how many radio buttons in this vector anyway?
    Mark Space, Apr 9, 2008
    1. Advertisements

  3. marcussilfver

    Mark Space Guest

    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
    Mark Space, Apr 9, 2008
  4. That will be hard to extract, it a rather complex application
    About 15-20 (depending on the model firing the updates)
    marcussilfver, Apr 9, 2008
  5. I did not cut'n paste the code sample, thats just a typo in my post,
    real code is with capital M.
    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
    You mean the addMouseListener(new MouseAdapter() code line? Sure I
    will give it a try!
    marcussilfver, Apr 9, 2008
  6. marcussilfver

    Mark Space Guest

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

    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.
    Mark Space, Apr 9, 2008
  7. 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.

    marcussilfver, Apr 10, 2008
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.