Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: MouseListener accumulating when setting new MouseListeners with mouseClicked()

  1. #1
    Junior Member
    Join Date
    Sep 2020
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default MouseListener accumulating when setting new MouseListeners with mouseClicked()

    Hi,

    I am a java beginner. I have an annoying problem . Im trying to make a card game with labels.

    My constructor, which has the GUI, has mouseListeners on the card labels... which can set other mouse listeners on new cards...for example when I click a card to play it...or when i draw a fresh one( which need to be made clickable)

    My question: how can I set the mouse listeners from executing multiple times at a single click ? First click works normally, but next onex executes 2, 4, 6 etc times , at a single click....playing multiple cards at a time

    basically I want to be able to do some stuff when clicking AND refresh listeners ( or put new ones on new cards )...but they should execute only once per click

    Any help ?

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: MouseListener accumulating when setting new MouseListeners with mouseClicked()

    The Component class has several methods that can be used to control its listeners: add..., get... and remove...
    Use those methods as needed for you application. For example if you want to replace an existing listener with a new listener, get the current listener, remove it and add the new listener.

    Note: it is possible to have multiple listeners. Adding a new listener does not remove any current listeners.
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    benuu90 (September 18th, 2020)

  4. #3
    Junior Member
    Join Date
    Sep 2020
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: MouseListener accumulating when setting new MouseListeners with mouseClicked()

    Thanks.

    I saved in an array all labels, applied listener to all with a loop, added in another array the eligible labels for clicking, and I read the clicked label with an "if".

    Don't know if its the best solution, but it works flawlessly

    Thanks a lot

Similar Threads

  1. 2 MouseListeners at the same location in a layeredPane
    By javaAllDay in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 17th, 2019, 09:35 AM
  2. mouseClicked and color objects in arrays
    By IcedWings in forum Java Applets
    Replies: 2
    Last Post: March 8th, 2013, 07:28 AM
  3. Help with accumulating sum values in do while loop
    By steel55677 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: June 19th, 2011, 11:01 PM
  4. MouseListener accumulating mouse clicks
    By mycallsevern in forum What's Wrong With My Code?
    Replies: 5
    Last Post: February 19th, 2011, 02:15 PM
  5. Java Loops - accumulating totals
    By iv3java in forum Loops & Control Statements
    Replies: 0
    Last Post: December 14th, 2009, 10:10 PM

Tags for this Thread