views:

450

answers:

2

Hi, The question I have is exactly same in requirement as http://stackoverflow.com/questions/173579/how-to-pass-mouse-events-to-applications-behind-mine-in-c-vista , but I need the same for a Transparent Java UI. I can easily create a transparent Java UI using 6.0 but couldn't get any info about passing events through the app to any applications(say a browser) behind.

A: 

You could try using the Robot Class http://java.sun.com/javase/6/docs/api/java/awt/Robot.html which allows you to specify system events to the low level operating system, specifically things like mouse events.

aperkins
Robot can be used for generating system events but if my transparent frame is already at the location where i need to click, wouldn't the java frame again receive the event?
Varun
you can catch the even at the glass pane (read the java tutorial for this) and then simply consume the event.
Savvas Dalkitsis
you can temporarily hide the window. it will introduce some flicker i know but i cant think of something else....
Savvas Dalkitsis
Just because you have changed the opacity of the window does not mean the events get transferred through. you would then need to fire the events using the robot class.
aperkins
A: 

I believe this will answer your question. To run it you will need Java 6 update 10 and above. I tested it on Windows Vista

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class CickThrough {

    public static void main(String[] args) {
     JFrame.setDefaultLookAndFeelDecorated(true);
     JFrame f = new JFrame("Test");
     f.setAlwaysOnTop(true);
     Component c = new JPanel() {
      @Override
      public void paintComponent(Graphics g) {
       Graphics2D g2 = (Graphics2D)g.create();
       g2.setColor(Color.gray);
       int w = getWidth();
       int h = getHeight();
       g2.fillRect(0, 0, w,h);
       g2.setComposite(AlphaComposite.Clear);
       g2.fillRect(w/4, h/4, w-2*(w/4), h-2*(h/4));
      }
     };
     c.setPreferredSize(new Dimension(300, 300));
     f.getContentPane().add(c);
     f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     f.pack();
     f.setVisible(true);
     com.sun.awt.AWTUtilities.setWindowOpaque(f,false);
    }

}

Note that you need to either have an undecorated window or one that is decorated by Java alone (not the default OS decoration) otherwise the code won't work.

Savvas Dalkitsis