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.


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

Results 1 to 2 of 2

Thread: Question about MouseListener

  1. #1
    Join Date
    Jul 2011
    Thanked 0 Times in 0 Posts

    Default Question about MouseListener

    I have a problem with using MouseListener.
    I am working to make a simulator for some electronic devices. I have some buttons for each kind of component I want to draw on a Canvas area. And if I click one of them, a String will take value the name of that component. So, then when I click back on the Canvas, the component with the name of the String will be drawn at the location that is clicked, and after that my String will become null.
    And I want to make those components to be selectable. When I click on one component it should become temporary to be blue(they are usually black). For that, then when my String is null, and if I click on the component, it will become blue.
    But something bad happens. I use MouseListener interface so i can listen the click events.
    I have tried both methonds of this interface:

    public void mousePressed(MouseEvent e)

    and this

    public void mousePressed(MouseEvent e)

    and that is how I noticed that each time i click both methods are run both each 2 times. So, after I add a component on my Canvas, it will become selected because after the first run of method, my String becomes null and it will do the selection thing. How can I avoid this?

  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Washington, DC
    My Mood
    Thanked 636 Times in 540 Posts

    Default Re: Question about MouseListener

    If you want help, you'll have to provide an SSCCE that demonstrates exactly what you're talking about. We don't need (or want) all of your code, just the fault behavior.

    By the way, both of the code pieces you posted above are identical. Is that what you intended?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. MouseListener Conundrum
    By Dirnol in forum Java Theory & Questions
    Replies: 6
    Last Post: October 11th, 2011, 07:11 AM
  2. 3Dmouse mouselistener
    By zimzille in forum Java Theory & Questions
    Replies: 0
    Last Post: April 6th, 2011, 03:12 AM
  3. Help with mouselistener and mousemotionlistener pls..
    By rentaw02 in forum Member Introductions
    Replies: 1
    Last Post: March 18th, 2011, 05:45 PM
  4. MouseListener
    By _lithium_ in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 18th, 2011, 11:43 AM
  5. MouseListener & MouseMotionListener
    By JavaLearner in forum AWT / Java Swing
    Replies: 9
    Last Post: March 26th, 2010, 07:18 AM