I am developing an application in which I have two JPanels (aPanel and bPanel) added to a parent JPanel (cPanel). I need to be able to double click on either aPanel or bPanel to change its color. If I double click on aPanel, it changes color as desired. When I double click on bPanel, its color does not change but aPanel's does. I have tried using the following methods to ID the panels: contains(), getComponentAt() and findComponentAt() but all give the same undesired behavior. I hope someone can point me in the right direction. The relevant code section is provided. (The code compiles but the "beep" never executes)

if(aPanel.findComponentAt(me.getX(), me.getY())==aPanel){
   aPanel.setBackground(chosenColor);
}            
                
else  if(bPanel.findComponentAt(me.getX(), me.getY())==bPanel){
    Toolkit.getDefaultToolkit().beep(); //to test if this is being executed
    bPanel.setBackground(chosenColor);
}

The "me" comes from public void mouseClicked(MouseEvent me), and another section of the code that uses "me" is working fine.

there areSwingUtilities more than two methods (child to parent, parent v.s. childs, getDeepestComponentAt)

I found another way to solve this problem:

if(me.getSource()==aPanel){         
                    aPanel.setBackground(chosenColor);
                }            
              else if(me.getSource()==bPanel){
                    bPanel.setBackground(chosenColor);
                             }

... too hard to pretend tha we are neither psychic nor sitting in front of your monitor...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.