Java GUI - MouseListener and ActionListener are possible to in same class?

actionlistener, java, mouselistener, swing, user-interface

Solution

It is definetly possible, just declare

private class CircleListener implements MouseListener, ActionListener

You can create two classes instead, and that is actually what I prefer, because then you have two distinct entities with clearly defined purposes. Each entity is responsible for just one function.

But both approaches are valid.

Problem

CircleListener is an inner class in my panel class and it involves MouseListener interface now. MouseRelased method checks if the clicked area is encircled by a circle and if so it sets that shape to selected and removes the selected ones. Now I need to an ActionListener to add random sized circles to this panel with a "timer" object. Question: Is it possible to implement "ActionListener" to CircleListener or it is better to create another inner class for "ActionListener"? Thanks in advance ``` private class CircleListener implements MouseListener { ShapesCanvas canvas; ShapeContainer container; Shape possibleShape; private CircleListener(ShapesCanvas canv, ShapeContainer cont) { this.canvas = canv; this.container = cont; } public void MouseRelased (MouseEvent e) { possibleShape = container.contains( e.getX(), e.getY()); if( possibleShape != null) { ( (Selectable)possibleShape).setSelected(true); container.removeSelected(); } canvas.repaint(); //repaints the last situation } ```

Original source