Java Swing Mouse addMouseListenerToAll(Component parent, MouseListener listener)

Here you can find the source of addMouseListenerToAll(Component parent, MouseListener listener)

Description

add Mouse Listener To All

License

Open Source License

Declaration

public static void addMouseListenerToAll(Component parent, MouseListener listener) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.awt.Component;
import java.awt.Container;

import java.awt.event.MouseListener;

import javax.swing.AbstractButton;
import javax.swing.JComponent;

public class Main {
    public static void addMouseListenerToAll(Component parent, MouseListener listener) {
        if (parent instanceof AbstractButton) {
            AbstractButton a = (AbstractButton) parent;
            boolean is = false;
            MouseListener[] kl = a.getMouseListeners();
            for (MouseListener k : kl) {
                if (k.equals(listener)) {
                    is = true;//w  w w  .  jav a 2  s .  c  o  m
                    break;
                }
            }
            if (!is)
                a.addMouseListener(listener);
        }

        else if (parent instanceof JComponent) {
            JComponent a = (JComponent) parent;
            boolean is = false;
            MouseListener[] kl = a.getMouseListeners();
            for (MouseListener k : kl) {
                if (k.equals(listener)) {
                    is = true;
                    break;
                }
            }
            if (!is)
                a.addMouseListener(listener);
        }

        if (parent instanceof Container) {
            Component[] comps = ((Container) parent).getComponents();
            for (Component c : comps) {
                addMouseListenerToAll(c, listener);
            }
        }
    }
}

Related

  1. adaptEventToDescendent(MouseEvent e, JComponent descendentTarget)
  2. addMouseListenerToHierarchy(JComponent c, MouseListener listener)
  3. animateMouse(Robot robot, JComponent jc, boolean moveMouse, boolean clickOnArrival)
  4. clickedInSelection(MouseEvent e)
  5. convert(MouseEvent event, Component newSource)