Search code examples
javajframetransparentjna

Clicking Past a Transparent Frame (Java)


I am trying to develop an application in Java (Swing) that lets me overlay a grid on the screen, and be able to click things behind the grid -- say, an icon the desktop for example.

Would I approach this problem with a Transparent JFrame or a Transparent JWindow?

Whatever I use, the transparent window/frame needs to

  • always be on top.
  • occupy the entire screen.
  • register every click with a mouse listener.
  • record the coordinates of the clicks on the screen.
  • allow me to use the Graphics class to draw a grid on the screen, and other elements, like numbers or images, that should also be click through.

Any direction would be appreciated.

Apologize if I haven't been specific enough, but I haven't found a demo window or frame that can do all these things. There's an example here and another here -- but I don't know how to use WindowUtils in Eclipse. This is my first time in GUI development and I've never used external libraries aside from the base Java classes.


Solution

    • on top: Frame.setAlwaysOnTop() should work for you
    • full screen: Setting the window size to the display size will make it occupy the entire screen
    • events and overlay painting: The actual behavior may vary by platform, but typically if you're using an alpha component to draw into your window, and your window is nominally transparent, those areas not painted (or sometimes those painted below a certain alpha threshold) will pass events through to whatever applications, windows, or components are underneath.
      If you capture events, you then have to re-introduce them to whatever window is below yours, which is non-trivial. If you don't capture events, you need to install an OS-specific event handler to capture events of interest.

    JNA's WindowUtils.setWindowTransparent() should provide the paint/event behavior required, or you can use the AWTUtils equivalent provided in more recent JVM releases.