Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / April 2008

Tip: Looking for answers? Try searching our database.

I am getting multiple events when clicking my radiobutton

Thread view: 
marcussilfver@gmail.com - 08 Apr 2008 20:30 GMT
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
Mark Space - 09 Apr 2008 01:19 GMT
> 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?
marcussilfver@gmail.com - 09 Apr 2008 06:07 GMT
> marcussilf...@gmail.com wrote:
> > I guess I have multiple threads somehow and from what I have read,
[quoted text clipped - 9 lines]
> 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)
Mark Space - 09 Apr 2008 01:58 GMT
> 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.
marcussilfver@gmail.com - 09 Apr 2008 06:19 GMT
> 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



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.