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
    13,773
    Blog Entries
    7
    Rep Power
    21

    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
    cenosillicaphobia: the fear for an empty beer glass

  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
    7

    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, 08:10 AM
  3. MouseClicked Stop Animation
    By xpngamer in forum New To Java
    Replies: 1
    Last Post: 04-14-2009, 10:38 AM
  4. Replies: 6
    Last Post: 04-06-2009, 12:28 AM

Posting Permissions

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