Java SwingUtilities get deepest component at point

Description

Java SwingUtilities get deepest component at point

import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseEvent;

import javax.swing.AbstractButton;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.SwingUtilities;
import javax.swing.event.MouseInputAdapter;

public class Main {
   public static void main(String[] args) {
      JFrame frame = new JFrame("GlassPaneDemo");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

      JCheckBox changeButton = new JCheckBox("Glass pane visible");
      changeButton.setSelected(false);/*from ww w .j  a  v  a 2  s.com*/

      Container contentPane = frame.getContentPane();
      contentPane.setLayout(new FlowLayout());
      contentPane.add(changeButton);
      contentPane.add(new JButton("Button 1"));
      contentPane.add(new JButton("Button 2"));

      JMenuBar menuBar = new JMenuBar();
      JMenu menu = new JMenu("Menu");
      menu.add(new JMenuItem("Do nothing"));
      menuBar.add(menu);
      frame.setJMenuBar(menuBar);

      MyGlassPane myGlassPane = new MyGlassPane(changeButton, frame.getContentPane());
      changeButton.addItemListener(myGlassPane);
      frame.setGlassPane(myGlassPane);

      frame.pack();
      frame.setVisible(true);
   }
}

class MyGlassPane extends JComponent implements ItemListener {
   Point point;

   public MyGlassPane(AbstractButton aButton, Container contentPane) {
      My listener = new My(aButton, contentPane);
      addMouseListener(listener);
      addMouseMotionListener(listener);
   }
   public void itemStateChanged(ItemEvent e) {
      setVisible(e.getStateChange() == ItemEvent.SELECTED);
   }

   protected void paintComponent(Graphics g) {
      if (point != null) {
         g.setColor(Color.red);
         g.fillOval(point.x - 10, point.y - 10, 20, 20);
      }
   }

   public void setPoint(Point p) {
      point = p;
   }

   class My extends MouseInputAdapter {
      Component liveButton;
      Container contentPane;

      public My(Component liveButton, Container contentPane) {
         this.liveButton = liveButton;
         this.contentPane = contentPane;
      }
      public void mouseClicked(MouseEvent e) {
         redispatchMouseEvent(e, false);
      }

      public void mousePressed(MouseEvent e) {
         redispatchMouseEvent(e, false);
      }
      private void redispatchMouseEvent(MouseEvent e, boolean repaint) {
         Point glassPanePoint = e.getPoint();
         Container container = contentPane;
         Point containerPoint = SwingUtilities.convertPoint(MyGlassPane.this, glassPanePoint, contentPane);
         if (containerPoint.y < 0) {
         } else {
            Component component = SwingUtilities.getDeepestComponentAt(container, containerPoint.x, containerPoint.y);
            if ((component != null) && (component.equals(liveButton))) {
               Point componentPoint = SwingUtilities.convertPoint(MyGlassPane.this, glassPanePoint, component);
               component.dispatchEvent(new MouseEvent(component, e.getID(), e.getWhen(), e.getModifiers(),
                     componentPoint.x, componentPoint.y, e.getClickCount(), e.isPopupTrigger()));
            }
         }
         if (repaint) {
            MyGlassPane.this.setPoint(glassPanePoint);
            MyGlassPane.this.repaint();
         }
      }
   }
}



PreviousNext

Related