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?
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
Re: Two Different MouseListener Actions on mouseClicked()
I'm not sure as to how to change its state. How would you do that?
Re: Two Different MouseListener Actions on mouseClicked()
Quote:
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:
Code:
if (isSelected)
{
panel.setBorder(oneColor);
isSelected = true;
}
else
{
panel.setBorder(anotherColor);
isSelected = false;
}
Re: Two Different MouseListener Actions on mouseClicked()
Thank you. It was simple, but for some reason i couldn't realize to do that.:(handshake):