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.