Identifying Swing component at a particular screen coordinate? (And manually dispatching MouseEvents)

前端 未结 3 683
北恋
北恋 2021-01-11 15:01

I\'m doing some work making a Java app compatible with alternative input devices. Unfortunately, the device in question has a Java API that\'s barely into the alpha stages r

3条回答
  •  没有蜡笔的小新
    2021-01-11 15:43

    In AWT Container, call this ...

    findComponentAt(int x, int y) 
              Locates the visible child component that contains the specified position
    

    i.e. If it is in a GlassPane...

      public static Component findComponentUnderGlassPaneAt(Point p, Component top) {
        Component c = null;
    
        if (top.isShowing()) {
          if (top instanceof RootPaneContainer)
            c =
            ((RootPaneContainer) top).getLayeredPane().findComponentAt(
                SwingUtilities.convertPoint(top, p, ((RootPaneContainer) top).getLayeredPane()));
          else
            c = ((Container) top).findComponentAt(p);
        }
    
        return c;
      }
    

    Reading your question, this might be of help to you also...

    If you want to exercise the control use this ... Java.awt.Robot class is used to take the control of mouse and keyboard. Once you get the control, you can do any type of operation related to mouse and keyboard through your java code. This class is used generally for test automation.

提交回复
热议问题