I have a Java program consisting of several class.
Now, I made the interface of the Java program in the class called Application. I have defined several buttons there.
This is how I added the mouse listeners to the buttons in the Application class:
I have another class called DrawingCanvas which contains, amongst others, a number of mouse event handlers. One of these event handlers is mousePressed. Here is the code of mousePressed:
The problem with mousePressed is that it is not recognizing the button names such as "Button_Square", "Button_Rectangle" etc. This is because these buttons have been declared in the Application class and NOT the DrawingCanvas class.
How can I get the name of the button which triggerred the mouse event please? Don't forget that the class in which the buttons have been declared is NOT the same as the class which implements the mouselisteners and mousemotionlisteners. Thanks :)
As long as you're going to create 8 different listeners, differentiate them with the JButton.
Button_Square.addMouseListener(new DrawingCanvas(Button_Square));
Button_Square.addMouseMotionListener(new DrawingCanvas(Button_Square));
Button_Rectangle.addMouseListener(new DrawingCanvas(Button_Rectangle));
Button_Rectangle.addMouseMotionListener(new DrawingCanvas(Button_Rectangle));
Button_Circle.addMouseListener(new DrawingCanvas(Button_Circle));
Button_Circle.addMouseMotionListener(new DrawingCanvas(Button_Circle));
Button_Triangle.addMouseListener(new DrawingCanvas(Button_Triangle));
Button_Triangle.addMouseMotionListener(new DrawingCanvas(Button_Triangle));