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..!

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Christian 'fuzi' Orgler
  • 1,682
  • 8
  • 27
  • 51
  • 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. – Andrew Thompson Jan 08 '11 at 01:44
  • I think this can help you: http://java.sun.com/developer/technicalArticles/GUI/translucent_shaped_windows/ –  Apr 23 '11 at 07:56

3 Answers3

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.

Tomek
  • 181
  • 1
  • 3
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.

LudoMC
  • 750
  • 8
  • 17
  • if i write "setUndecorated" then it throws a exception: "IllegalComponentStateException: The dialog is displayable" – Christian 'fuzi' Orgler Jan 07 '11 at 23:12
  • 1
    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. – LudoMC Jan 07 '11 at 23:36
  • 1
    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. – LudoMC Jan 08 '11 at 02:33
  • thanks.. i made it with a JWindow.. the decoration is gone - but the background is visible... *hm* – Christian 'fuzi' Orgler Jan 10 '11 at 02:57
  • 1
    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 (http://java.sun.com/developer/technicalArticles/J2SE/Desktop/javase6/splashscreen/) to see if you can reuse something. – LudoMC Jan 10 '11 at 19:17
  • `setBackground`, as in @Tomek's answer, is also required for it to work. – Mark Jeronimus Feb 20 '17 at 15:26
  • Almost nine years later, this does not work for me when the LookAndFeel i set to Nimbus. Any ideas please, @LudoMC – gbenroscience Nov 13 '19 at 00:45
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 ! :)

猫IT
  • 412
  • 5
  • 11
  • 1
    This may be better as a comment. You should certainly upvote the original article link if it helped you. – Michael Brewer-Davis Jun 22 '11 at 02:49
  • 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. – 猫IT Jun 29 '11 at 01:38