-2

I have added 2 JComponent to JPanel.Both these JComponent have custom painted objects on them i.e multiple Circles and multiple Line2Ds.The dimensions of both these jcomponent's are set to the size of the screen which is required.When I add them to JPanel,the mouselisteners are not working.However if I comment one of them then that listener starts working.

public class MapXMLBuilder extends JFrame {
        public void initialize() {
            panel = new Jpanel();
            addLine();//ading lines
            addCircle();//adding cirlces
            add(panel)
        }
        private void addLine() {
            mDrawLine = new DrawLine(panel);
            mDrawCircle.setDimensions(screenWidth, screenWidth);
            panel.add(mDrawLine); //adding JComponent1 to panel
            for (int i = 0; i < 10; i++) { //adding 10 circles       
                mDrawLine.addLine(new Point2D.Double(x, y));
            }
        }
        private void addCircle() {
            mDrawCircle = new DrawCircle(panel);
            mDrawCircle.setDimensions(screenWidth, screenWidth);
            panel.add(mDrawCircle); //adding JComponent1 to panel
            for (int i = 0; i < 10; i++) { //adding 10 circles       
                mDrawCircle.addCircle(new Point2D.Double(x, y));
            }
        }
    }

    public class DrawLine extends JComponent {
        public DrawLine(JPanel jpanel) {
            lineList = new ArrayList();
            addMouseListener(mouseAdapter);
            addMouseMotionListener(mouseAdapter);
            setBackground(Color.white);
        }
        public void addLine(int x1, int y1, int x2, int y2, Color color) {
            Shape currentShape = new Line2D.Float(x1, y1, x2, y2);
            lineList.add(currentShape);
            repaint();
        }
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g;
            g2d.setPaint(Color.BLACK);
            for (Shape shape: lineList) {
                if (shapes != null && shapes.size() > 0) {
                    g2d.setColor(Color.YELLOW);
                    g2d.draw(shape);
                }
            }
        }
        public void setDimensions(int width, int height) {
            setSize(new Dimension(width, height));
        }
    }

    public class DrawCircle extends JComponent {
        public DrawCircle(JPanel jpanel) {
            circleList = new ArrayList();
            addMouseListener(mouseAdapter);
            addMouseMotionListener(mouseAdapter);
            setBackground(Color.white);
        }
        public void addCircle(Point2D p) {
            Ellipse2D e = new Ellipse2D.Double(p.getX(), p.getY(), 5, 5);
            circleList.add(e);
            selectedPoint = null;
            repaint();
        }
        public void setDimensions(int width, int height) {
            setSize(new Dimension(width, height));
        }
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2 = (Graphics2D) g;
            int diameter = 75;
            Ellipse2D e;
            Color color;
            for (int j = 0; j < circleList.size(); j++) {
                e = (Ellipse2D) circleList.get(j);
                float alpha = 0.75f;
                color = new Color(1, 0, 0, alpha); //Red 
                g2.setPaint(color);
                Ellipse2D.Double circle = new Ellipse2D.Double(e.getX(), e.getY(), diameter, diameter);
                g2.fill(circle);
            }
        }
    }
Raj Trivedi
  • 557
  • 7
  • 18
  • If they both take up the whole screen and must be on top of each other AND must be added to the same component, do they really need to be separate components? – pvg Dec 21 '15 at 13:45
  • subpanels.setOpaque(false) ? – FredK Dec 21 '15 at 15:20
  • The question is why? Why not combine the painting functionality into a single component – MadProgrammer Dec 21 '15 at 20:48
  • there are Line2ds and circle.they each have mouselisteners implemented. If a shape is moved,the properties associated with it also changes and they both have different set of properties. Hence,I feel to keep them seperate as otherwise I would have to spend lot of time determining what is moved as sometimes the shapes may overlap. Also,what if tomorrow some more shapes are added to the screen. The code will become too complex I guess. – Raj Trivedi Dec 22 '15 at 03:47
  • 1
    Post an [MCVE](http://stackoverflow.com/help/mcve). Be sure to copy-paste your code to a *new project* and make sure it compiles and runs before posting it here. – user1803551 Dec 22 '15 at 12:37
  • @user1803551-I have added the code above,Sorry I cannot add complete code due to security issues.Now,the shapes are getting added to the screen but the mouselisteners on them are not working. – Raj Trivedi Dec 24 '15 at 03:59

1 Answers1

0

You can forward the event from one to the other. Try something like this:

firstComponent.addMouseListener(new MouseAdapter(){
    public void mouseClicked(MouseEvent event){
        // forward the event
        secondComponent.dispatchEvent(event);

        // handle the code here
    }
});
secondComponent.addMouseListener(// etc. 
Leo Izen
  • 4,165
  • 7
  • 37
  • 56
  • I went through the demo of glass pane.it either keeps the root pane or the glass pane active at a time.what if we want both the components to be active simultaneously? – Raj Trivedi Dec 22 '15 at 04:09
  • I have edited my answer above.The 2 shapes are getting added but the mouselisteners on them are not working – Raj Trivedi Dec 24 '15 at 04:11
  • I edited my answer to match the question I think you're asking. – Leo Izen Dec 25 '15 at 06:19
  • If an answer is the "correct answer" then you should accept it: http://stackoverflow.com/help/accepted-answer – Leo Izen Dec 31 '15 at 19:14