How to set transparent background of JDialog
Asked Answered
C

3

10

Hy,..

how can i set the background transparent and "remove" the closeoperation (marked red) ? I only want to show the card :-)

alt text

Thanks..!

Coetaneous answered 7/1, 2011 at 22:53 Comment(2)
Note that top-level containers like JDialog, JFrame and JApplet were not intended to be transparent. There was a hack mentioned in a Sun article to allow transparency and curved windows (using com.sun classes), but it stopped working. Java 7 is supposed to reintroduce (into the J2SE) translucent/transparent TLCs.Entophyte
I think this can help you: java.sun.com/developer/technicalArticles/GUI/…Use
B
12

yourDialog.setUndecorated(true)should do the trick for the title bar.

For having the Frame transparent. You'll have to work on the root panel with yourDialog.getRootPane().setOpaque(false)on it.

Burnejones answered 7/1, 2011 at 22:55 Comment(7)
if i write "setUndecorated" then it throws a exception: "IllegalComponentStateException: The dialog is displayable"Cuthbert
Argh, I'm not on my development environment right now. I'll test and come back when I can put my hands on the right computer.Burnejones
Until I can do a test, you should perhaps try with a JWindow (which comes by default undecorated). You should perhaps also have a look at the SplashScreen functionality of Java6.Burnejones
thanks.. i made it with a JWindow.. the decoration is gone - but the background is visible... hmCuthbert
Hi, I've then checked in our code base. We are using an undecorated JFrame as the AboutBox. The image is taking the full part of the panel and is rectangular so we don't have the problem of the transparency. As Andrew said, as of today, it will be closer to hack then anything else to make it work as of today. As said, you could perhaps try to dig in the SplashScreen (java.sun.com/developer/technicalArticles/J2SE/Desktop/javase6/…) to see if you can reuse something.Burnejones
setBackground, as in @Tomek's answer, is also required for it to work.Impatience
Almost nine years later, this does not work for me when the LookAndFeel i set to Nimbus. Any ideas please, @BurnejonesGarniture
M
17

Although there is no problem with UNDECORATED JFrame transparency (myJFrame.setBackground (new Color (0,0,0,0)); is pretty enough), the same with JDialog is not working.

I discovered, however, the following sequence works perfect for JDialog:

myJDialog.getRootPane ().setOpaque (false);
myJDialog.getContentPane ().setBackground (new Color (0, 0, 0, 0));
myJDialog.setBackground (new Color (0, 0, 0, 0));

A also remain, but it is my PRIVATE, humble suggestion, that all setBackground call for Window extenders (e.g. JFrame, JDialog) should be tried against UnsupportedOperationException and IllegalComponentStateException.

Morissa answered 15/3, 2012 at 9:54 Comment(4)
You Sir Just made my Day :)Farrah
I think it is better if I receive the exception and fix it.Pfosi
getContentPane().setBackground was not required in my caseImpatience
I was getting the error java.awt.IllegalComponentStateException: The dialog is decorated at java.awt.Dialog.setBackground. I had to replace the second setBackground with myJDialog.setUndecorated(true);Jolynnjon
B
12

yourDialog.setUndecorated(true)should do the trick for the title bar.

For having the Frame transparent. You'll have to work on the root panel with yourDialog.getRootPane().setOpaque(false)on it.

Burnejones answered 7/1, 2011 at 22:55 Comment(7)
if i write "setUndecorated" then it throws a exception: "IllegalComponentStateException: The dialog is displayable"Cuthbert
Argh, I'm not on my development environment right now. I'll test and come back when I can put my hands on the right computer.Burnejones
Until I can do a test, you should perhaps try with a JWindow (which comes by default undecorated). You should perhaps also have a look at the SplashScreen functionality of Java6.Burnejones
thanks.. i made it with a JWindow.. the decoration is gone - but the background is visible... hmCuthbert
Hi, I've then checked in our code base. We are using an undecorated JFrame as the AboutBox. The image is taking the full part of the panel and is rectangular so we don't have the problem of the transparency. As Andrew said, as of today, it will be closer to hack then anything else to make it work as of today. As said, you could perhaps try to dig in the SplashScreen (java.sun.com/developer/technicalArticles/J2SE/Desktop/javase6/…) to see if you can reuse something.Burnejones
setBackground, as in @Tomek's answer, is also required for it to work.Impatience
Almost nine years later, this does not work for me when the LookAndFeel i set to Nimbus. Any ideas please, @BurnejonesGarniture
G
2

I followed the instructions from the article and it worked finnaly AND it wasn't difficult at all. :) I now have my translucent SplashImage ans About screen which displays a PNG image and respect its (complex) transparency. Just awesome. Note that the method to proceed will change a little bit in JDK 7.

Just notice the difference between keywords. http://download.oracle.com/javase/tutorial/uiswing/misc/trans_shaped_windows.html#6u10

It would have been nice if the

yourDialog.setUndecorated(true);
yourDialog.getRootPane().setOpaque(false);

trick worked but it didn't to me. Maybe I did something wrong.

I also note it is important to use setContentPane(Component); instead of getContentPane.add(Component);

I'm happy it works now ! :)

Gearbox answered 22/6, 2011 at 2:12 Comment(2)
This may be better as a comment. You should certainly upvote the original article link if it helped you.Maturation
Yes it was more like a comment rather than an answer. I'm pretty newbie here i didn't understood much how the site is organised, sorry. :) By the way I succeed is translucency under windows but I do not have AWTUtilities under Linux so I just can't do this.Welby

© 2022 - 2024 — McMap. All rights reserved.