Results 1 to 5 of 5
- 01-01-2012, 06:02 PM #1
Member
- Join Date
- Aug 2011
- Posts
- 9
- Rep Power
- 0
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?
- 01-01-2012, 06:11 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,397
- Blog Entries
- 7
- Rep Power
- 17
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 01-01-2012, 06:57 PM #3
Member
- Join Date
- Aug 2011
- Posts
- 9
- Rep Power
- 0
Re: Two Different MouseListener Actions on mouseClicked()
I'm not sure as to how to change its state. How would you do that?
- 01-01-2012, 08:33 PM #4
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
Re: Two Different MouseListener Actions on mouseClicked()
Keep a boolean variable, maybe named isSelected.I'm not sure as to how to change its state. How would you do that?
Then when you click the first time you do:
Java Code:if (isSelected) { panel.setBorder(oneColor); isSelected = true; } else { panel.setBorder(anotherColor); isSelected = false; }
- 01-01-2012, 09:55 PM #5
Member
- Join Date
- Aug 2011
- Posts
- 9
- Rep Power
- 0
Similar Threads
-
Java MouseClicked problem.
By Love Like Rockets. in forum AWT / SwingReplies: 12Last Post: 10-30-2011, 01:58 PM -
getClickCount() problem within mouseClicked method
By mitra in forum AWT / SwingReplies: 4Last Post: 08-10-2011, 07:10 AM -
MouseClicked Stop Animation
By xpngamer in forum New To JavaReplies: 1Last Post: 04-14-2009, 09:38 AM -
Need help with drawing multiple objects with mouseClicked method.
By busdude in forum New To JavaReplies: 6Last Post: 04-05-2009, 11:28 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks