Transparent JFrame using JNA

In Make JFrame transparent I had shown a way to making frame’s transparent using AWTUtilities class. But using that class results in access restriction compile time error, resolution in Eclipse is also shown in that post. Now here is the version using java natives. I have used Java Native Access (JNA) library to call native functions to get the things done.

What is Java Native Access (JNA)?

JNA provides Java programs easy access to native shared libraries (DLLs on Windows) without writing anything but Java code—no JNI or native code is required.JNA allows you to call directly into native functions using natural Java method invocation.

The Code

import javax.swing.JFrame;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import com.sun.jna.platform.WindowUtils;

public class TransparentFrame extends JFrame {
   public TransparentFrame() {
      setTitle('Transparent Frame');
      setSize(400,400);
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      JSlider slider = new JSlider(JSlider.HORIZONTAL, 30, 100, 100);

      slider.addChangeListener(new ChangeListener() {
         @Override
         public void stateChanged(ChangeEvent e) {
            JSlider slider = (JSlider) e.getSource();
            if(!slider.getValueIsAdjusting()){
               WindowUtils.setWindowAlpha(TransparentFrame.this, slider.getValue()/100f);
            }
         }
      });
      add(slider);
      setVisible(true);
   }
   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
  @Override
  public void run() {
     new TransparentFrame();
  }
      });
   }
}

Here WindowUtils class is provided in JNA jar (platform.jar). The method setWindowAlpha of WindowUtils class is used to make a window transparent. First argument of this method is your frame/window and second argument is alpha value. This class also has a method called setWindowTransparent, which can also be used to make window transparent .

Dependencies

You will need following 2 jars to run this program: (Both jar files are available to download on GitHub for JNA.)

  • jna.jar
  • platform.jar

To run above code on Windows, you will need to set “sun.java2d.noddraw” system property before calling the WindowUtils function.

System.setProperty('sun.java2d.noddraw', 'true');

Output

Additional Notes

I have tested this code on following machines:

  • Windows XP service pack 3 (32 bit)
  • Windows 7 (32 bit)
  • Cent OS 5 (32 bit)

If you test it on other machines or have code for other machines using JNA for the same functionality then feel free to share it as comment to this post.

Happy coding and don’t forget to share!

Reference: Transparent JFrame using JNA from our JCG partner Harsh Raval at the harryjoy blog.

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

JPA Mini Book

Learn how to leverage the power of JPA in order to create robust and flexible Java applications. With this Mini Book, you will get introduced to JPA and smoothly transition to more advanced concepts.

JVM Troubleshooting Guide

The Java virtual machine is really the foundation of any Java EE platform. Learn how to master it with this advanced guide!

Given email address is already subscribed, thank you!
Oops. Something went wrong. Please try again later.
Please provide a valid email address.
Thank you, your sign-up request was successful! Please check your e-mail inbox.
Please complete the CAPTCHA.
Please fill in the required fields.

One Response to "Transparent JFrame using JNA"

  1. Justan says:

    Does this allow for per-pixel transparency?

Leave a Reply


+ six = 8



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy | Contact
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.
Do you want to know how to develop your skillset and become a ...
Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

Get ready to Rock!
You can download the complementary eBooks using the links below:
Close