Results 1 to 5 of 5
  1. #1
    5myl is offline Member
    Join Date
    Aug 2011
    Posts
    9
    Rep Power
    0

    Question Two Different MouseListener Actions on mouseClicked()

    Hello, I am fairly inexperienced with Swing, as i have just started playing around with it a few days ago. I came across a problem with MouseListener. I want to be able to have two different mouseClicked() events, when the first time a panel is clicked, it is selected, as it will be drawn with a different colored border. Then, when it is clicked a second time, it is deselected. I thought of maybe using two different mouse listeners, but they would both happen at the same time then, because i do not know a way to let the computer distinguish between the two different clicks. How would you go about doing this?

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,044
    Blog Entries
    7
    Rep Power
    23

    Default Re: Two Different MouseListener Actions on mouseClicked()

    Remember that a mouse listener is just a class object, so it can have state; so you can solve your problem with one mouse listener that keeps (and changes) its state.

    kind regards,

    Jos
    The only person who got everything done by Friday was Robinson Crusoe.

  3. #3
    5myl is offline Member
    Join Date
    Aug 2011
    Posts
    9
    Rep Power
    0

    Default Re: Two Different MouseListener Actions on mouseClicked()

    I'm not sure as to how to change its state. How would you do that?

  4. #4
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    8

    Default Re: Two Different MouseListener Actions on mouseClicked()

    I'm not sure as to how to change its state. How would you do that?
    Keep a boolean variable, maybe named isSelected.

    Then when you click the first time you do:

    Java Code:
    if (isSelected)
    {
        panel.setBorder(oneColor);
        isSelected = true;
    }
    else
    {
        panel.setBorder(anotherColor);
        isSelected = false;
    }

  5. #5
    5myl is offline Member
    Join Date
    Aug 2011
    Posts
    9
    Rep Power
    0

    Default Re: Two Different MouseListener Actions on mouseClicked()

    Thank you. It was simple, but for some reason i couldn't realize to do that.

Similar Threads

  1. Java MouseClicked problem.
    By Love Like Rockets. in forum AWT / Swing
    Replies: 12
    Last Post: 10-30-2011, 02:58 PM
  2. Replies: 4
    Last Post: 08-10-2011, 07:10 AM
  3. MouseClicked Stop Animation
    By xpngamer in forum New To Java
    Replies: 1
    Last Post: 04-14-2009, 09:38 AM
  4. Replies: 6
    Last Post: 04-05-2009, 11:28 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •