5

I'm trying to create a jframe that the user can click through. I'm not looking for opacity but transparency.

I need a solution that works on all OS and not just Windows because I can't use

WindowUtils.setWindowTransparent(frame, true);
WindowUtils.setWindowAlpha(frame, 0.6f);

or

AWTUtilities.setWindowOpaque(this, false);
AWTUtilities.setWindowOpacity(this, 0.8f);

Can I accomplish this with java alone? It's ok if there is a library that I must use.

EDIT: I have my jframe undecorated and here is the code for it.

frame = new JDialog();
frame.setUndecorated(true);
frame.setVisible(true);
frame.setOpacity(Shared.opacity);
frame.setLocation(0, 0);
frame.setSize(Shared.screenWidth, Shared.screenHeight);

When I say the user can click through what I mean is that if my frame is on top but they have a window under mine, clicking on mine would bring the one under on top.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
user2612619
  • 1,119
  • 3
  • 11
  • 28

1 Answers1

4

A completely transparent window can be achieved in Java 7 by using a completely transparent background color, for example...

JFrame frame = new JFrame("Testing");
frame.setUndecorated(true);
frame.setBackground(new Color(0, 0, 0, 0));

The problem you will have is, that anywhere there is any kind of solid pixel (even if it is transparent), it will stop the mouse events from going beyond the window...

Transparent

This means that every child component you add to the frame (that you want to be able to click through) will need to be transparent as well.

I use a similar technique for some of my utility programs and include a MouseListener on the main opaque component to make the window more visible so I can drag it if I want to it, but that's stuff for another question ;)

import java.awt.AlphaComposite;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class ClickThroughWindow {

    public static void main(String[] args) {
        new ClickThroughWindow();
    }

    public ClickThroughWindow() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setUndecorated(true);
                frame.setBackground(new Color(0, 0, 0, 0));
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.setAlwaysOnTop(true);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
            setOpaque(false);
            setLayout(new GridBagLayout());
            add(new JLabel("Hello"));
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 200);
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g.create();
            g2d.setColor(getBackground());
//            g2d.setComposite(AlphaComposite.SrcOver.derive(0.5f));
//            g2d.fillRect(0, 0, getWidth(), getHeight());
            g2d.setColor(Color.BLACK);
            g2d.drawRect(0, 0, getWidth() - 1, getHeight() - 1);
            g2d.dispose();
        }

    }

}

You can find more details at How to Create Translucent and Shaped Windows, in particular How to Implement Per-Pixel Translucency

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • While that does create a window that is translucent it does not allow me to click through it. – user2612619 Dec 27 '13 at 22:32
  • In fact, it creates a completely transparent window which you can click through. As I stated, *"The problem you will have is, that anywhere there is any kind of solid pixel (even if it is transparent), it will stop the mouse events from going beyond the window..."*, the moment you have a window with any pixel whose alpha value is greater than 0, it will block the mouse events (hence the reason for the commented code in the `paintComponent` method). There's nothing else you can do. – MadProgrammer Dec 27 '13 at 22:41
  • It cannot be clicked through, but your answer about it blocking mouse events is helpful. Is there anyway around this? This is not a solution because I can't click through the window. – traderjosh Oct 01 '17 at 13:47