Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

JavaWorld Daily Brew

MouseListener Outer JPanel with InnerPanels



Hi,

i'm new in GUI programming with JAVA and Netbeans, so i`ve got a problem that i can't solve:

In fact i have a JPanel (OuterPanel) that contains many other JPanels (Inner), the InnerPanels have a Tooltipp. Now the Problem is that my mouselistener that i added to the outer panel doesn't react if i click in the panel but on a inner panel.
how can i solve that.

for example:

public class Main {

    public static void main(String args[]) {
        MyJFrame frame = new MyJFrame();
        MyJPanel pan = new MyJPanel(); //outer JPanel
        pan.addMouseListener(new MouseAdapter() {

            @Override
            public void mouseClicked(MouseEvent e) {
                System.out.println("Clicked:"+e.getClickCount());
            }
          
        });
      
        frame.add(pan);
        frame.setVisible(true);
     }
}
//_________________________________________________

public class MyJPanel extends JPanel{

    public MyJPanel() {
        setBackground(Color.green);

        Color[] colors = {
            Color.white, Color.lightGray, Color.gray, Color.darkGray,
            Color.black, Color.red, Color.pink, Color.orange,
            Color.yellow, Color.green, Color.magenta, Color.cyan, Color.blue
        };

        //fügt neun kästchen hinzu
        for (int i=0; i<9; i++) {
            JPanel test = new JPanel();
            test.setBackground(colors[i]);
            test.setToolTipText(colors[i].toString());
            this.add(test);
        }
   }
}
//____________________________________________________________
public class MyJFrame extends JFrame {
  
    public MyJFrame() {
        setTitle("MyJFrame");
        setSize(300,300);
     }
}

greetings#>