Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set transparent background of JDialog

Hy,..

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

alt text

Thanks..!

like image 957
Christian 'fuzi' Orgler Avatar asked Jan 07 '11 22:01

Christian 'fuzi' Orgler


People also ask

How do I open JDialog in center of screen?

If you really want to center a JDialog on screen, you can use code like this: // center a jdialog on screen JDialog d = new JDialog(); d. setSize(400, 300); d. setLocationRelativeTo(null); d.

What is the use of JDialog?

Class JDialog. The main class for creating a dialog window. You can use this class to create a custom dialog, or invoke the many class methods in JOptionPane to create a variety of standard dialogs. For information about creating dialogs, see The Java Tutorial section How to Make Dialogs.

How do I exit JDialog?

All you need to do is to create a custom subclass of JDialog , define an ActionListener for closing the JDialog , and register the Escape keystroke to call the action listener.


3 Answers

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.

like image 152
Tomek Avatar answered Oct 10 '22 08:10

Tomek


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.

like image 26
LudoMC Avatar answered Oct 10 '22 08:10

LudoMC


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 ! :)

like image 43
猫IT Avatar answered Oct 10 '22 09:10

猫IT