Java Swing JDialog Transparency - java

I have been having this problem for quite some time and I've been unable to find the solution anywhere on the internet.
I have a JDialog as my main frame and I need to be able to change the transparency of the window. Normally this would not be a problem if I was to call setUndecorated(true) on it, but I do not want to do that. I want the title bar to appear at the top under the system look and feel.
The interesting part is that on my Mac, changing the transparency works just fine with the system look and feel, even though the JDialog is still "decorated".
Here's an image of my JDialog so you know what I'm dealing with:
I need to use the system look and feel, but I also need to be able to change the transparency of the window.
When I try, I get the following exception on windows:
"Exception in thread "AWT-EventQueue-0" java.awt.IllegalComponentStateException: The dialog is decorated"
If I set it to undecorated, I lose the title bar at the top which is not an option. Anybody know a way around this?

Related

Java Swing JDialog - how to modify after it is displayed? [duplicate]

I am developing a Java Swing-based application with different perspectives. For the "main menu" perspective I do not want the window (JFrame) to be decorated, while in other perspective I do want the window to be decorated. In other words, I need want to change the decoration attribute dynamically.
I have tried to use setUndecorated(false) and setUndecorated(true), but I seems I can only set this once, before actually showing the window.
Is there a way to achieve this?
From the javadoc:
Disables or enables decorations for this frame. This method can only be called while the frame is not displayable.
Therefore, once the JFrame is packed and/or displayed, you can no longer change that value. If you want to change the undecorated state of a JFrame you will need to dispose() it first, then change the state and eventually make it visible again.
After all, I had to take a different approach.
The former solution did work, as I stated in my last comment.
However, it was showing the default LAF window decoration, while I was using a different LAF.
So the result was graphically inconsistent with the rest of the LAF. Finally, I came with the right solution, I used setUndecorate(true) for my frame. Then, when I had to change my perspective to one using decorations I simply had to use the following code
contentPane.getRootPane().setWindowDecorationStyle(JRootPane.FRAME);
And when I had to revert to the non decorate perspective, I use
contentPane.getRootPane().setWindowDecorationStyle(JRootPane.NONE);
This approach didn't need to dispose the window and show it again (which actually produced a brief but still visible hide/show of the window)

Recreate Windows Look-and-Feel border of JFrame or JDialog?

I need to recreate the translucent border of JFrame or JDialog that appears when setting the windows look and feel in swing.
I need it because Windows LaF does not let you access the title bar (on the border). In fact, I need to apply a MouseAdapter to the JDialog that gets notified when it is dragged/pressed/released. In windows laf, as you cannot get access to the bar component, you can only apply a ComponentListener which gives you notification only when moving (so you don't capture anything when the user has grabbed it but hasn't moved yet, or either when the user "releases" it).
Therefore, I decided to go with undecorated dialogs and apply the listeners to my custom bar. However I want the custom dialog looks exactly as in windows laf (it means I need to recreate the border).
I'm not very experienced in Graphics2D to override the paintBorder() method, so I'm asking for your help.
Has anyone ever faced this problem and has a tested solution?
As of the Java Platform, Standard Edition 6 (Java SE 6) Update 10 release, you can add translucent and shaped windows to your Swing applications.
This means that you can have your JFrame emulate a native window with the rounded corners and transparency.
In your case, your approach would be in the JFrame level instead of the border level because the border is painted on top of the JFrame (or JDialog, for that matter). Therefore, if the JFrame is not already rounded, for instance, the paintBorder() method will still be painting on top of a rectangular area of the screen.
Check this tutorial from Oracle covering shaped and translucent JFrame.

Dynamically show and hide JFrame decorations

I am developing a Java Swing-based application with different perspectives. For the "main menu" perspective I do not want the window (JFrame) to be decorated, while in other perspective I do want the window to be decorated. In other words, I need want to change the decoration attribute dynamically.
I have tried to use setUndecorated(false) and setUndecorated(true), but I seems I can only set this once, before actually showing the window.
Is there a way to achieve this?
From the javadoc:
Disables or enables decorations for this frame. This method can only be called while the frame is not displayable.
Therefore, once the JFrame is packed and/or displayed, you can no longer change that value. If you want to change the undecorated state of a JFrame you will need to dispose() it first, then change the state and eventually make it visible again.
After all, I had to take a different approach.
The former solution did work, as I stated in my last comment.
However, it was showing the default LAF window decoration, while I was using a different LAF.
So the result was graphically inconsistent with the rest of the LAF. Finally, I came with the right solution, I used setUndecorate(true) for my frame. Then, when I had to change my perspective to one using decorations I simply had to use the following code
contentPane.getRootPane().setWindowDecorationStyle(JRootPane.FRAME);
And when I had to revert to the non decorate perspective, I use
contentPane.getRootPane().setWindowDecorationStyle(JRootPane.NONE);
This approach didn't need to dispose the window and show it again (which actually produced a brief but still visible hide/show of the window)

How to set JInternalFrame minimised title background?

I can set the title bar background when maximised using InternalFrame.activeTitleBackground and InternalFrame.inactiveTitleBackground but how do I set it when the internal frame is minimised?
There is no easy solution. The frame colors are part of the UI, so you will need to create a custom UI to do what you want. You can start by searching the Java source code to see where the "activeTitleBackground" and "inactiveTitleBackground" properties are used. That will give you an idea of the class and method that needs to be customized.

How to show a shape or graphic in java with no window frame?

I'm having a hard time trying to make a Java program with just a shape (e.g rectangle) as main window. I don't want the shape to be inside the 'OS-window' (with buttons to close and minimize etc..)
I don't know if you can draw directly on the screen in Java (I tend to think you can't). But you could create a JDialog (which doesn't appear in the taskbar) and call setUndecorated(true) on it (to get rid of the title bar). Then you can do whatever custom painting you want with it.
Edit: kts points out that JWindow will work even better for this purpose. From the Javadocs:
It does not have the title bar, window-management buttons, or other trimmings associated with a JFrame, but it is still a "first-class citizen" of the user's desktop, and can exist anywhere on it.
And there's even a no-argument constructor, so you don't have to worry about passing in a null owner!
For more exotic shapes: translucent and shaped windows. Works only in 6u10 and later.

Categories