Emulate This SWT Shell in Swing
Asked Answered
S

3

5

I've been experiencing with SWT (GUI lib Eclipse uses) and I'm wanting to create the following using Swing.

enter image description here

The previous screenshot is done by the following code using SWT

public static void main(String[] args) {
    Display display = new Display();
    final Shell shell = new Shell(display, SWT.RESIZE);
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

I was wondering, how could I possible emulate this in Swing?

P.S The border around this Shell is currently the native border for my windows color scheme, I don't wanna just create a MatteBorder and emulate the color, I'd like to use the native border for windows.

Stockman answered 18/7, 2014 at 1:9 Comment(8)
Eclipse also use SWT over SwingReckless
Undecorate the JDialog, then create your own borderProctology
@VinceEmigh, yes that has already been suggested 25 minutes earlier. Did you even read the answer?Ezarras
Another way would be to use a JWindow, which can be used for implementing a widget.Rafter
@Ezarras If I didn't, I would have posted it as an answer. I posted the comment to inform him how it's done in Java, seeing how he didn't approve of your answer; the way you suggested isn't the only way to create a border. Also, I suggest pointing out the fact that its not possible dynamically usin JDialog, so he understands why you suggested what you did.Proctology
I would be fine without using a JDialog, just need to know what type of swing component can emulate the same look.Stockman
@JonathanBeaudoin, just need to know what type of swing component can emulate the same look - the Border API as has already been suggested. There is no LAF Border you can just plug in, you need to write your own if you don't like the standard Borders of the JDK.Ezarras
If you want things to look like SWT you should note that this SWT dialog will have completely different borders on different platforms (on a Mac for example it has no border at all).Stickinthemud
L
3

So the question is: Can native-styled windows with borders only be created using Swing?
Shortly: No. Unfortunately it is impossible to do this using Swing.
Also: Same "No" goes for the tool windows which can also be created using SWT.

Now let me explain why.

Swing uses pure-Java written rendering to paint any components and custom decorations within your application. Window decoration in Swing is a bit different topic though - by default JFrame and JDialog are decorated using native style - basically when window instance is created in your system from Java application - that window is asked to have default decoration for either frame or dialog (plus some other possible options). After that this window is used to render Swing components on it. But Swing code has no real control over the system-provided decoration, it can only switch it on/off and configure a few options in it. Unfortunately the option you are looking for is not there - most probably wasn't not implemented as not very popular one.

Though Swing allows custom component/window styling through custom L&F (Look and Feel). L&F which supports custom window decorations simply provides its own way to paint and control window decoration. In case such L&F is installed - Swing uses undecorated frames/dialogs by default and simply asks L&F to do the decoration. Anyone can write their own L&F which means anyone can create custom decorations.

So as @camickr mentioned before you have only one option if you want to create such frame (like on the screenshot in your post) in Swing - use either JDialog/JFrame with setUndecorated set to true or JWindow which is undecorated by default and "paint your way through". Graphics2D provided for all painting operations will allow you to create any custom decoration of your window, even the one on your screenshot.

I won't lie - creating custom window decoration is pretty difficult - it requires a lot of knowledge in Swing and large amount of written code, not only painting code. So I really doubt you will choose that option unless you are passionate about UI creation.

In the end - you have to choose between SWT and Swing (or even JavaFX) as each of these UI frameworks offer totally different sets of features and options. Window decoration is just a tip of the large iceberg.

EDIT: Since JDK 7 it is actually prossible to create tool windows, all you need is to set your window type to Window.Type.UTILITY. Though it is still not possible to create a window with native borders only without any title bar.

Lampas answered 23/7, 2014 at 15:11 Comment(0)
E
3

Use an undecorated JDialog. Then you can add a MatteBorder to the root pane of the dialog. Or you can always implement you own custom Border to emulate the LAF Border.

Ezarras answered 18/7, 2014 at 1:27 Comment(2)
That's not what I'm trying to do. I want to add the LAF border to the outside of it, just like on regular windows.Stockman
@JonathanBeaudoin, There is a difference between what you want and what you can do. What you want is not support. I offered an alternative.Ezarras
L
3

So the question is: Can native-styled windows with borders only be created using Swing?
Shortly: No. Unfortunately it is impossible to do this using Swing.
Also: Same "No" goes for the tool windows which can also be created using SWT.

Now let me explain why.

Swing uses pure-Java written rendering to paint any components and custom decorations within your application. Window decoration in Swing is a bit different topic though - by default JFrame and JDialog are decorated using native style - basically when window instance is created in your system from Java application - that window is asked to have default decoration for either frame or dialog (plus some other possible options). After that this window is used to render Swing components on it. But Swing code has no real control over the system-provided decoration, it can only switch it on/off and configure a few options in it. Unfortunately the option you are looking for is not there - most probably wasn't not implemented as not very popular one.

Though Swing allows custom component/window styling through custom L&F (Look and Feel). L&F which supports custom window decorations simply provides its own way to paint and control window decoration. In case such L&F is installed - Swing uses undecorated frames/dialogs by default and simply asks L&F to do the decoration. Anyone can write their own L&F which means anyone can create custom decorations.

So as @camickr mentioned before you have only one option if you want to create such frame (like on the screenshot in your post) in Swing - use either JDialog/JFrame with setUndecorated set to true or JWindow which is undecorated by default and "paint your way through". Graphics2D provided for all painting operations will allow you to create any custom decoration of your window, even the one on your screenshot.

I won't lie - creating custom window decoration is pretty difficult - it requires a lot of knowledge in Swing and large amount of written code, not only painting code. So I really doubt you will choose that option unless you are passionate about UI creation.

In the end - you have to choose between SWT and Swing (or even JavaFX) as each of these UI frameworks offer totally different sets of features and options. Window decoration is just a tip of the large iceberg.

EDIT: Since JDK 7 it is actually prossible to create tool windows, all you need is to set your window type to Window.Type.UTILITY. Though it is still not possible to create a window with native borders only without any title bar.

Lampas answered 23/7, 2014 at 15:11 Comment(0)
P
2

On Eclipse I'm pretty sure it's done with an org.eclipse.swt.widgets.Shell with SWT.Border as the style. I can't think of a way to do this in Swing. You could write one yourself but it wouldn't have the system LAF.

If this is really important I would suggest using SWT instead, but that might require a re-write. Otherwise you could use a SWT Shell with Swing components embedded in it, but in my experience this is extremely fiddly and hard to debug.

Proliferation answered 18/7, 2014 at 2:16 Comment(1)
Besides embedding Swing in SWT you could also try drawing it yourself, like this but without the title and buttons: Frosted Glass on the Java™ Desktop. Like I said, I don't think this is something Swing can do out of the box.Proliferation

© 2022 - 2024 — McMap. All rights reserved.