Question

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 :)

Was it helpful?

Solution 2

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));

OTHER TIPS

Well for a start, you can make a single DrawingCanvas and make that a field in your application class; here I'm going to call it canvas. Now, if all of your buttons do different things, you can give them all different MouseListeners:

Button_Rectangle.addMouseListener(new MouseListener()
{
    public void mousePressed(MouseEvent e)
    {
           if(!e.isControlDown())
           {
                 canvas.Shapes.add(new Rectangle(e.getX(), e.getY()));
           }
    }
    //handle other events, or don't directly implement MouseListener

});

Buuton_Circle.addMouseListener(new MouseListener()
{
    public void mousePressed(MouseEvent e)
    {
           if(!e.isControlDown())
           {
                 canvas.Shapes.add(new Circle(e.getX(), e.getY()));
           }
    }
    //handle other events, or don't directly implement MouseListener

});

so on...

Eliminating the need to determine the source by using different objects.

You can read text that you have set in the button. If that text is unique, it will identify the button. The following is logic I am using to identify and control JToggleButtons.

Note that once you have the Abstract button you might be able to do what you want with the button (e.g. get/set background colors) without parsing the text property.

Example:

void PrintEventProperties(java.awt.event.MouseEvent evt) {
    AbstractButton EventSource = (AbstractButton)evt.getSource();
    System.out.println("Toggled on? " + EventSource.isSelected() );
    System.out.println("ID/text=" + EventSource.getText() );
    System.out.println("Color=" + EventSource.getBackground() );
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top