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) { System.out.println("~~~~"); }
and this
public void mousePressed(MouseEvent e) { System.out.println("~~~~"); }
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?