1

I made a program in Java in which you can draw Ellipses and Rectangles, then later group them to do Resizes and Move actions on them.

I add MouseListeners to both the DrawPanel(JPanel) as all the created Shapes(Components on that drawPanel). But after I group several shapes together, it seems like the MouseListener is pushed backwards and the MouseListener linked to MainFrame fires.

I have made a workaround the calls .GetCompontent() function on the JPanel. But is must surely be possible to push forward a Components MouseListener?

Both the JPanel and the Components on it use the same MouseListener

public class MainFrame extends JFrame implements ActionListener,MouseInputListener {        

public MainFrame() {
    super("Tekenprogramma");
    //some other stuf
    drawPanel.addMouseListener(this);
    drawPanel.addMouseMotionListener(this);
}

and when shapes are created and added in the same class:

public void mousePressed(MouseEvent e) {
    if(e.getSource() == drawPanel) {
        Point pos = e.getPoint();
        switch(mode) {
//Some other cases...
        case RECTANGLE:
            shape = new Rectangle(pos);
            shape.addMouseListener(this);
            shape.addMouseMotionListener(this);
            storeAndExecute(new RectangleCommand(drawPanel,shape));

initially, the shapes do react to their own listener. But after I remove and read them (nested in a component without MouseListener (group)) They stop firing and the drawPanel's MouseListener takes over.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Sjaak van der Heide
  • 566
  • 2
  • 6
  • 21
  • 1
    possible duplicate of [Java event propagation stopped](http://stackoverflow.com/questions/3605086/java-event-propagation-stopped) –  Mar 01 '12 at 15:06
  • It's a Swing design principle that only one component receives an event. What you have is a propagation precedence problem. –  Mar 01 '12 at 15:08

0 Answers0