I've been experiencing with SWT (GUI lib Eclipse uses) and I'm wanting to create the following using Swing.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With