How to make JFrame transparent? I want to make my JFrame transparent. User should see the background when my JFrame is on top of it.
Asked
Active
Viewed 3.7k times
9
-
possible duplicate of [Transparent JFrame background](http://stackoverflow.com/questions/2533650/transparent-jframe-background) – Jul 12 '11 at 07:47
-
An easy tutorial - https://www.youtube.com/watch?v=zecGJfNHPWo – m4heshd May 22 '17 at 22:40
2 Answers
15
I found another solution.
Set the background color of your frame to
// Set the frame background color to a transparent color
yourFrameHere.setBackground(new Color(0, 0, 0, 0));
And remember to set the opacity off of the contentpane (your JPanel or other component)
// turn off opacity of the content pane
yourContentPaneHere.setOpaque(false);

drzymala
- 2,009
- 20
- 26
-
1
-
It is a method of the abstract class JComponent. http://docs.oracle.com/javase/7/docs/api/javax/swing/JComponent.html#setOpaque(boolean) So, every JPanel and other components have this method. – drzymala May 10 '15 at 12:14
-
When I try to do this, I get a ```java.awt.IllegalComponentStateException``` with the message ```The frame is displayable```. How do I fix that? I tried to call ```setBackground()``` before any other calls such as ```pack()``` but it didn't help. – qwerty Jun 10 '20 at 14:02
9
If you do not have any objection in using restricted API classes then you can do this with AWTUtilities
class and setWindowOpacity()
method of that class. Here and here is a tutorial on how to use it? And here is the version using Java native access.
code example
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
javax.swing.JFrame fr = new NewJFrame();
com.sun.awt.AWTUtilities.setWindowOpacity(fr, 0.7 f);
fr.setVisible(true);
}
});
}

Birju Vachhani
- 6,072
- 4
- 21
- 43

Harry Joy
- 58,650
- 30
- 162
- 207
-
public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { javax.swing.JFrame fr = new NewJFrame(); com.sun.awt.AWTUtilities.setWindowOpacity(fr,0.7f); fr.setVisible(true); } }); } – Costis Aivalis Jul 12 '11 at 08:51
-
I didn't want to do that since it is just meant to enhance your answer... Do not know how to format the code in a comment. – Costis Aivalis Jul 12 '11 at 09:12
-
-
Suppose you didnt want to use restricted API's, is there another way to do this? – user489041 Jul 12 '11 at 14:56
-
-
nice blog post :-) Though you might consider to add a reference to the original article at java.net as there as well. – kleopatra Dec 05 '11 at 10:25
-
-
@kleopatra: I will update post to have reference to java.net as soon as possible. Java 7 is still not used much so most of people avoid it for now. – Harry Joy Dec 05 '11 at 10:30
-
I must note, that since using this, I was only able to get it to work on Windows. It seems it is not available in Linux. This was using java 1.6.20. Might be different with java 7. Haven't used it yet. – user489041 Dec 05 '11 at 15:55
-
The setWindowOpacity is not suitable for mac, I can't down vote the answer as I don't have enough points yet but martini's answer is much better. – Justinfront Jul 20 '13 at 05:56