Note that there are some explanatory texts on larger screens.

plurals
  1. POJava: Making a window click-through (including text/images)
    primarykey
    data
    text
    <p>I want to create an overlay in Java that is <strong>transparent</strong>, <strong>always on top</strong>, and that I can <strong>click-through</strong>. I've found some <a href="https://stackoverflow.com/questions/4167664/ability-to-click-through-a-java-app">similar</a> <a href="https://stackoverflow.com/questions/10465203/opaque-components-on-transparent-java-windows">posts</a> about this issue, but even after following their answers, I'm having one issue.</p> <p>My problem is making the <strong>whole window</strong> click-through. I'm not having any problem making it work with a JFrame, but once I <strong>add any components</strong> to it (JLabel or an ImagePanel), the <strong>click-through attribute doesn't carry over</strong> to them.</p> <p>As I want to have a background image for my application this basically makes the code I have useless seeing how the window gets focused whenever I click the area the text/image covers.</p> <p>Before I show the code I'm using I'd first like to refer to <a href="https://stackoverflow.com/questions/527933/how-to-make-a-click-and-see-through-always-on-top-window">these</a> <a href="https://stackoverflow.com/questions/112224/click-through-transparency-for-visual-c-sharp-window-forms">threads</a> which essentially describes precisely what I want, except in C#.</p> <p>My goal is to create an overlay with a transparent .png-image and some text on-top that will change on key events. If it uses JFrame or any other library doesn't matter. I only need it compatible with Windows.</p> <p>I'd also like to mention that I've got some experience with Java, but am a novice in using JFrame.</p> <pre><code>import java.awt.BorderLayout; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.SwingConstants; import com.sun.jna.platform.WindowUtils; public class Overlay { public static void main(String[] args) { JFrame frame = new JFrame("Overlay Window"); frame.setUndecorated(true); frame.setAlwaysOnTop(true); frame.getRootPane().putClientProperty("apple.awt.draggableWindowBackground", false); frame.setLocation(400, 400); frame.getContentPane().setLayout(new java.awt.BorderLayout()); JLabel textLabel = new JLabel("I'm a label in the window", SwingConstants.CENTER); frame.getContentPane().add(textLabel, BorderLayout.CENTER); frame.pack(); System.setProperty("sun.java2d.noddraw", "true"); WindowUtils.setWindowTransparent(frame, true); WindowUtils.setWindowAlpha(frame, 1.0f); //Using AWTUtilities gives the same result as WindowUtils //AWTUtilities.setWindowOpaque(frame, false); //AWTUtilities.setWindowOpacity(frame, 1.0f); frame.setVisible(true); } } </code></pre> <p>Note that the problem is <strong>not about the window being focused</strong> (though that is a result of the issue), but about the <strong>JLabel and ImagePanel not being click-through</strong>.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload